From a9785cc64c565db301c8d08f652cea24b3399eaa Mon Sep 17 00:00:00 2001 From: ManuelW Date: Sat, 11 May 2024 10:16:57 +0200 Subject: [PATCH] DB config --- insertLoading.py | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/insertLoading.py b/insertLoading.py index c8a442d..886684a 100644 --- a/insertLoading.py +++ b/insertLoading.py @@ -17,26 +17,16 @@ except: exit(0) -try: - db = sqlite3.connect(db_file) - cursor = db.cursor() - print("Connected to SQLite") -except: - print("Failed to connect to DB") - ## ### auf neue Files überwachen def main(): - global db, cursor try: - updateLast() - db.commit() - print("Data updated successfully into bike_loading table") + db = sqlite3.connect(db_file) + cursor = db.cursor() + print("Connected to SQLite") - insertNew() - db.commit() - print("Data inserted successfully into bike_drives table") - cursor.close() + updateLast(db, cursor) + insertNew(db, cursor) except sqlite3.Error as error: print("Failed to insert data into sqlite table", error) @@ -46,8 +36,8 @@ def main(): print("The SQLite connection is closed") -def updateLast(): - global db, cursor, watt +def updateLast(db, cursor): + global watt ## hole letztes Ladungs-Datum sql = ''' @@ -90,10 +80,12 @@ def updateLast(): WHERE id = ? ''' cursor.execute(sql, data) + db.commit() + print("Data updated successfully into bike_loading table") -def insertNew(): - global db, cursor, loading +def insertNew(db, cursor): + global loading date = datetime.today().strftime('%Y-%m-%d %H:%M:%S') data = (date, loading) @@ -105,6 +97,9 @@ def insertNew(): ''' cursor.execute(sql, data) + db.commit() + print("Data inserted successfully into bike_drives table") + cursor.close() if __name__ == '__main__':