-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_db.py
79 lines (69 loc) · 1.9 KB
/
create_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sqlite3
con = sqlite3.connect("db.sqlite3")
cur = con.cursor()
# Creating our tables
def _createTable(table_name: str, script: str):
try:
cur.execute(f"SELECT * FROM {table_name}")
print(f"{table_name} already exists.")
except sqlite3.OperationalError:
msg = ""
try:
print(f"Creating {table_name} table...")
cur.execute(script)
msg += "Successfully created "
except:
msg += "Failed to create "
msg += f"new {table_name} table."
print(msg)
# For lego_group
group_script = """
CREATE TABLE IF NOT EXISTS lego_group (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE
);
"""
_createTable("lego_group",group_script)
# For lego_theme
theme_script = """
CREATE TABLE IF NOT EXISTS lego_theme (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
group_id REFERENCES lego_group (id) ON DELETE CASCADE
);
"""
_createTable("lego_theme",theme_script)
# For lego_series
series_script = """
CREATE TABLE IF NOT EXISTS lego_series (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
theme_id REFERENCES lego_theme (id) ON DELETE CASCADE
);
"""
_createTable("lego_series",series_script)
# For lego_kit - Might need to fix this
# * complete_kit should be a boolean value default as false
# * for_sale should be a boolean value default as false
kit_script = """
CREATE TABLE IF NOT EXISTS lego_kit (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE,
number_sets INTEGER DEFAULT 1,
complete_kit INTEGER DEFAULT 0,
for_sale INTEGER DEFAULT 0,
box_location TEXT,
notes TEXT
series_id REFERENCES lego_series (id) ON DELETE CASCADE
);
"""
_createTable("lego_kit",kit_script)
# For lego_parts
# * id (part number) - unique
# * name - unique
# For parts_to_kits
# * kit_id - foreign key
# * part_id - foreign key
# * number_of_parts - default at 1
con.commit()
con.close()