2021-08-27 08:11:00 +00:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
def create_database(name):
|
|
|
|
return sqlite3.connect(name)
|
|
|
|
|
|
|
|
|
2021-08-27 08:55:31 +00:00
|
|
|
# better way to do the above
|
|
|
|
# def create_connection(db_file):
|
|
|
|
# """create a database connection to the SQLite database
|
|
|
|
# specified by db_file
|
|
|
|
# :param db_file: database file
|
|
|
|
# :return: Connection object or None
|
|
|
|
# """
|
|
|
|
# conn = None
|
|
|
|
# try:
|
|
|
|
# conn = sqlite3.connect(db_file)
|
|
|
|
# return conn
|
|
|
|
# except Error as e:
|
|
|
|
# print(e)
|
|
|
|
# return conn
|
|
|
|
|
|
|
|
|
2021-08-27 08:11:00 +00:00
|
|
|
def create_tables(db):
|
|
|
|
c = db.cursor()
|
|
|
|
c.execute(
|
|
|
|
""" CREATE TABLE IF NOT EXISTS Habits (
|
2021-08-27 21:33:52 +00:00
|
|
|
id integer PRIMARY KEY AUTOINCREMENT,
|
2021-08-27 08:11:00 +00:00
|
|
|
archived integer,
|
|
|
|
color integer,
|
|
|
|
description text,
|
|
|
|
freq_den integer,
|
|
|
|
freq_num integer,
|
|
|
|
highlight integer,
|
2021-08-27 21:33:52 +00:00
|
|
|
name text,
|
|
|
|
position integer,
|
2021-08-27 08:11:00 +00:00
|
|
|
reminder_hour integer,
|
|
|
|
reminder_min integer,
|
2021-08-27 21:33:52 +00:00
|
|
|
reminder_days integer NOT NULL DEFAULT 127,
|
|
|
|
type integer NOT NULL DEFAULT 0,
|
|
|
|
target_type integer NOT NULL DEFAULT 0,
|
|
|
|
target_value real NOT NULL DEFAULT 0,
|
|
|
|
unit text NOT NULL DEFAULT "",
|
2021-08-27 08:11:00 +00:00
|
|
|
question text,
|
2021-08-27 21:33:52 +00:00
|
|
|
uuid text
|
2021-08-27 08:11:00 +00:00
|
|
|
); """
|
|
|
|
)
|
|
|
|
c.execute(
|
|
|
|
""" CREATE TABLE IF NOT EXISTS Repetitions (
|
2021-08-27 21:33:52 +00:00
|
|
|
id integer PRIMARY KEY AUTOINCREMENT,
|
|
|
|
habit integer NOT NULL REFERENCES Habits(id),
|
|
|
|
timestamp integer NOT NULL,
|
|
|
|
value integer NOT NULL
|
2021-08-27 08:11:00 +00:00
|
|
|
); """
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-28 07:54:41 +00:00
|
|
|
def migrate(name):
|
2021-08-27 08:55:31 +00:00
|
|
|
db = create_database(name)
|
2021-08-27 08:11:00 +00:00
|
|
|
create_tables(db)
|
2021-08-27 08:55:31 +00:00
|
|
|
return db
|