diff --git a/my_movies/add_value.sql b/my_movies/add_value.sql new file mode 100644 index 0000000..bad0e52 --- /dev/null +++ b/my_movies/add_value.sql @@ -0,0 +1,2 @@ +INSERT INTO eggs (cooler_typ, glückszahl) +VALUES('{coolertype}', {eine_zahl}); \ No newline at end of file diff --git a/my_movies/create_db.sql b/my_movies/create_db.sql new file mode 100644 index 0000000..8311313 --- /dev/null +++ b/my_movies/create_db.sql @@ -0,0 +1,5 @@ + CREATE TABLE IF NOT EXISTS eggs( + cooler_typ TEXT, + glückszahl INTEGER + ); + \ No newline at end of file diff --git a/my_movies/database.py b/my_movies/database.py index ee2813f..c0406f4 100644 --- a/my_movies/database.py +++ b/my_movies/database.py @@ -3,25 +3,32 @@ 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 eggs( - cooler_typ TEXT, - glückszahl INTEGER - ); - ''' + fd = open(self.script_folder + os.sep + "create_db.sql", "r") + sql_code = fd.read() + #sql_code = ''' + # CREATE TABLE IF NOT EXISTS eggs( + # cooler_typ TEXT, + # glückszahl INTEGER + # ); + # ''' + cursor.execute(sql_code) con_db.commit() def add_value_db(self, cooler_typ = str, glueckszahl = int): with sqlite3.connect(self.db_file) as con_db: cursor = con_db.cursor() - sql_code = ''' - INSERT INTO eggs (cooler_typ, glückszahl) - VALUES('{coolertype}', {eine_zahl}); - '''.format(coolertype=cooler_typ,eine_zahl=glueckszahl) + fd = open(self.script_folder + os.sep + "add_value.sql", "r") + sql_code = fd.read().format(coolertype=cooler_typ,eine_zahl=glueckszahl) + #sql_code = ''' + # INSERT INTO eggs (cooler_typ, glückszahl) + # VALUES('{coolertype}', {eine_zahl}); + # '''.format(coolertype=cooler_typ,eine_zahl=glueckszahl) + cursor.execute(sql_code) con_db.commit() @@ -41,5 +48,5 @@ if __name__ == "__main__": db_file_full = script_folder + os.sep + "spam.db" my_db = db_work(db_file=db_file_full) my_db.create_db() - my_db.add_value_db(cooler_typ="daniel-2", glueckszahl=42) + my_db.add_value_db(cooler_typ="daniel-3", glueckszahl=42) print(my_db.show_value()) \ No newline at end of file diff --git a/my_movies/spam.db b/my_movies/spam.db index f92ff5f..a35d72a 100644 Binary files a/my_movies/spam.db and b/my_movies/spam.db differ