Files
python_skripte/my_movies/database.py

76 lines
3.1 KiB
Python

import sqlite3, os
class db_work:
def __init__(self,db_file = str ) -> None:
self.db_file = db_file
self.script_folder = os.path.dirname(os.path.realpath(__file__))
def create_db(self):
with sqlite3.connect(self.db_file) as con_db:
cursor = con_db.cursor()
sql_code = """
CREATE TABLE IF NOT EXISTS movies(
'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'name' TEXT,
'filmgenres' TEXT,
'year' TEXT,
'persönlich' TEXT
); """
cursor.execute(sql_code)
sql_code = """
CREATE TABLE IF NOT EXISTS filmgenres(
'genre' TEXT PRIMARY KEY,
'beschreibung' TEXT
); """
cursor.execute(sql_code
)
con_db.commit()
def add_value_db(self):
with sqlite3.connect(self.db_file) as con_db:
cursor = con_db.cursor()
while True:
name = input("Name des Films: ")
if not name:
break
movie_genre = input("Genre: ")
movie_year = input("Jahr: ")
per_info = input("Meinung : ")
sql = """Select * from movies WHERE name = '{name}';""".format(name=name)
cursor.execute(sql)
if not cursor.fetchone():
print("Film wird angelgt ")
self.speicher_aktion(movie=name, filmgenre=movie_genre, year=movie_year, persönlich=per_info)
else:
print("Film bereits vorhanden !")
print("Eingabevorgang wurde beendet.")
def speicher_aktion(self, movie = str, filmgenre = str, year = str, persönlich = str):
with sqlite3.connect(self.db_file) as verbindung:
cursor = verbindung.cursor()
sql = """INSERT INTO movies(name, filmgenres, year, 'persönlich' )
VALUES ( '{movie}', '{filmgenre}', '{year}', '{persönlich}' );""".format(
movie=movie,
filmgenre=filmgenre,
year=year,
persönlich=persönlich)
cursor.execute(sql)
def show_value(self, sql_query = str):
with sqlite3.connect(self.db_file) as con_db:
cursor = con_db.cursor()
sql_code = sql_query
cursor.execute(sql_code)
ergebnis = cursor.fetchall()
return ergebnis
if __name__ == "__main__":
script_folder = os.path.dirname(os.path.realpath(__file__))
db_file_full = script_folder + os.sep + "my_moviedb.db"
my_db = db_work(db_file=db_file_full)
my_db.create_db()
add_value_to_db = my_db.add_value_db()
#print (add_value_to_db)
print(my_db.show_value(sql_query="""SELECT * FROM movies;"""))