-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
99 lines (84 loc) · 2.79 KB
/
database.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
# Example script to set-up and create the necessary tables for the database.
# Also serves as the definition of keys and columns of each table.
import bcrypt
import sqlite3
conn = sqlite3.connect("db.db")
conn.execute(
"""
CREATE TABLE IF NOT EXISTS oncologists
(username TEXT NOT NULL,
password TEXT NOT NULL,
full_name TEXT NOT NULL,
is_admin INTEGER DEFAULT "FALSE" NOT NULL,
PRIMARY KEY(username));
"""
)
conn.execute(
"""
CREATE TABLE IF NOT EXISTS patients
(id INTEGER NOT NULL,
user_id TEXT NOT NULL,
name TEXT NOT NULL,
phone_number TEXT NOT NULL,
birthday TEXT NOT NULL,
age TEXT NOT NULL,
blood_type TEXT NOT NULL,
all_type TEXT NOT NULL,
weight TEXT NOT NULL,
height TEXT NOT NULL,
body_surface_area TEXT NOT NULL,
oncologist_id TEXT NOT NULL,
sex TEXT NOT NULL DEFAULT "Male",
PRIMARY KEY(id),
FOREIGN KEY(oncologist_id)
REFERENCES oncologists(username)
ON DELETE CASCADE
ON UPDATE NO ACTION);
"""
)
conn.execute(
"""
CREATE TABLE IF NOT EXISTS measurements
(time TEXT NOT NULL,
anc_measurement REAL NOT NULL,
dosage_measurement REAL NOT NULL,
patient_id INTEGER NOT NULL,
PRIMARY KEY(time, patient_id),
FOREIGN KEY(patient_id)
REFERENCES patients(id)
ON DELETE CASCADE
ON UPDATE NO ACTION);
"""
)
# INSERT examples are listed below, feel free to uncomment to insert any entries as needed.
# encrypting password so it's not stored in plain text
password = "password"
bytes = password.encode("utf-8")
salt = bcrypt.gensalt()
hash = bcrypt.hashpw(bytes, salt)
# conn.execute(
# '''
# INSERT INTO oncologists (username, password, full_name, is_admin)
# VALUES ('angus', ?, 'Angus Wang', 'FALSE')
# ''',
# (hash,),
# )
# conn.execute(
# """
# INSERT INTO patients (user_id, name, phone_number, birthday, age, blood_type, all_type, weight, height, body_surface_area, oncologist_id)
# VALUES ('smallbob123456', 'Small Bob', '1234567899', '19900506', 38, 'A+', 'Immunophenotype', 1, 1, 250, 'angus');
# """
# )
# conn.execute(
# """
# INSERT INTO measurements (time, anc_measurement, dosage_measurement, patient_id)
# VALUES ("20220101", 4, 4, 1);
# """
# )
conn.execute("PRAGMA foreign_keys = ON") # enable foreign key cascade on delete
# conn.execute(
# "DROP TABLE oncologists"
# )
conn.commit() # this is necessary to confirm entry into the database
conn.close()