import sqlite3 def migrate(db, habitlist, events): c = db.cursor() for event in events: text = event["note"] for habit in habitlist: tag = f"#{habit['description']}" if tag in text: print(f"register: {habit['name']} in {text} at {event['end']}") add_to_database(c, fetch_habit_id(c, habit), event["end"]) def fetch_habit_id(c, habit): c.execute("select id from Habits where uuid = ?", [(habit["uuid"])]) return c.fetchone()[0] # does not do: # ranges in habits (should check for habit being range, then taking #habit(3) number into account) # also, range trackers: multiply value by 1,000 # non-range habits but still #habit(3) number included, adding multiple? def add_to_database(cursor, habit_id, timestamp, value=2): try: cursor.execute( """ INSERT INTO Repetitions(id, habit, timestamp, value) VALUES (NULL, ?, ?, ?) """, (habit_id, timestamp, value), ) except sqlite3.IntegrityError: # TODO better error handling print("fail to register: not unique") pass