habitmove/loop/table_constructor.py

62 lines
2.1 KiB
Python
Raw Normal View History

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 (
id integer PRIMARY KEY,
archived integer,
color integer,
description text,
freq_den integer,
freq_num integer,
highlight integer,
name text NOT NULL,
position integer UNIQUE,
reminder_hour integer,
reminder_min integer,
reminder_days integer,
type integer,
target_type integer,
target_value real,
unit text,
question text,
uuid text NOT NULL
); """
)
c.execute(
""" CREATE TABLE IF NOT EXISTS Repetitions (
id integer PRIMARY KEY,
habit integer,
timestamp integer,
value integer
); """
)
2021-08-27 08:55:31 +00:00
def prepare_database(name):
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