54 lines
1.9 KiB
Python
54 lines
1.9 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()
|
|
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()
|
|
fd = open(self.script_folder + os.sep + "add_value.sql", "r")
|
|
sql_code = fd.read().format(coolertype=cooler_typ,eine_zahl=glueckszahl)
|
|
|
|
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()
|
|
sql_code = '''
|
|
SELECT * FROM eggs;
|
|
'''
|
|
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 + "spam.db"
|
|
my_db = db_work(db_file=db_file_full)
|
|
my_db.create_db()
|
|
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()) |