diff --git a/my_movies/create_db.sql b/my_movies/create_db.sql index 8311313..872dde5 100644 --- a/my_movies/create_db.sql +++ b/my_movies/create_db.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS eggs( - cooler_typ TEXT, + cooler_typ TEXT PRIMARY KEY, glückszahl INTEGER ); \ No newline at end of file diff --git a/my_movies/database.py b/my_movies/database.py index c0406f4..b76f23a 100644 --- a/my_movies/database.py +++ b/my_movies/database.py @@ -24,14 +24,15 @@ class db_work: cursor = con_db.cursor() 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) + + try: + cursor.execute(sql_code) + except sqlite3.IntegrityError: + return False + con_db.commit() - + return True + def show_value(self): with sqlite3.connect(self.db_file) as con_db: cursor = con_db.cursor() @@ -48,5 +49,6 @@ 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-3", glueckszahl=42) + add_value_to_db = my_db.add_value_db(cooler_typ="daniel-5", glueckszahl=42) + print (add_value_to_db) print(my_db.show_value()) \ No newline at end of file diff --git a/my_movies/spam.db b/my_movies/spam.db index 3bbf02c..c510326 100644 Binary files a/my_movies/spam.db and b/my_movies/spam.db differ diff --git a/play_with_sqllite/main.py b/play_with_sqllite/main.py new file mode 100644 index 0000000..4883f76 --- /dev/null +++ b/play_with_sqllite/main.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python.3.11 + +import sqlite3, os + +script_folder = os.path.dirname(os.path.realpath(__file__)) + +def erzeuge_tabellen(): + with sqlite3.connect(script_folder + "/startegie.db") as verbindung: + cursor = verbindung.cursor() + sql = ''' + CREATE TABLE viren( + name TEXT PRIMARY KEY, typ INTEGER, status TEXT); + ''' + cursor.execute(sql) + + sql = ''' + CREATE TABLE viren_typ( + typ INTEGER PRIMARY KEY,groesse INT,signatur TEXT); + ''' + cursor.execute(sql) + verbindung.commit() + +def schreibe_tabellen(): + with sqlite3.connect(script_folder + "/startegie.db") as verbindung: + cursor = verbindung.cursor() + sql = ''' + INSERT INTO viren_typ(typ, groesse, signatur) + VALUES(1,128,'ABAABA'),(2,256,'ABAABA'),(3,256,'BCCBCB') + ''' + cursor.execute(sql) + + sql = ''' + INSERT INTO viren(name, typ, status) + VALUES ('T800', 1, 'aktiv'), + ('T803', 2, 'aktiv'), + ('Bit13', 3, 'aktiv'), + ('Gorf3', 1, 'aktiv'), + ('Gorf7', 2, 'aktiv') + ''' + + cursor.execute(sql) + verbindung.commit() + + +def lese_tabellen(): + with sqlite3.connect(script_folder + "/startegie.db") as verbindung: + cursor = verbindung.cursor() + sql = '''SELECT * FROM viren_typ;''' + cursor.execute(sql) + #for ergebnis in cursor: + # print(ergebnis) + + return cursor.fetchall() + + + + +if __name__ == "__main__": +# erzeuge_tabellen() +# schreibe_tabellen() + for i in lese_tabellen(): + print(i) \ No newline at end of file diff --git a/play_with_sqllite/startegie.db b/play_with_sqllite/startegie.db new file mode 100644 index 0000000..db08a65 Binary files /dev/null and b/play_with_sqllite/startegie.db differ