diff --git a/parseFitFiles.py b/parseFitFiles.py index e604891..46c7da6 100644 --- a/parseFitFiles.py +++ b/parseFitFiles.py @@ -98,15 +98,30 @@ def doHealthFit(file, min_alt=0.0): def writeDB(): global fitData, db_file - db = sqlite3.connect(db_file) - cursor = db.cursor() - - data = [fitData["date"], fitData["total_distance"], fitData["total_ascent"], fitData["max_altitude"], fitData["avg_speed"], - fitData["avg_heart_rate"], fitData["total_calories"]] - - cursor.executemany('INSERT INTO bike_drives VALUES (?)', data) - db.commit() - db.close() + try: + db = sqlite3.connect(db_file) + cursor = db.cursor() + + sql = ''' + INSERT INTO bike_drives + (d_date, d_total_distance, d_total_ascent, d_max_alt, d_avg_speed, d_avg_hr, d_calories) + VALUES (?, ?, ?, ?, ?, ?, ?); + ''' + + data = (fitData["date"], fitData["total_distance"], fitData["total_ascent"], fitData["max_altitude"], fitData["avg_speed"], + fitData["avg_heart_rate"], fitData["total_calories"]) + + cursor.execute(sql, data) + db.commit() + print("Data inserted successfully into bike_drives table") + cursor.close() + + except sqlite3.Error as error: + print("Failed to insert data into sqlite table", error) + finally: + if db: + db.close() + print("The SQLite connection is closed") #row.extend(["Date", "Total Distance", "Total Ascent", "Max Alt.", "AVG Speed", "AVG HR", "Calories"])