Repository navigation
Expand file tree
/
Copy pathdb.py
More file actions
77 lines (62 loc) · 1.6 KB
/
Copy pathdb.py
File metadata and controls
77 lines (62 loc) · 1.6 KB
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
import sqlite3
import json
from datetime import datetime
DB_PATH = "mednerds.db"
def init_db():
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
patient_id TEXT,
age INTEGER,
sex TEXT,
history TEXT,
symptoms TEXT,
medication TEXT,
created_at DATETIME
)
""")
conn.commit()
conn.close()
def save_record(patient_id, final_df):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
row = final_df.iloc[0]
c.execute(
"""
INSERT INTO records (
patient_id, age, sex, history, symptoms, medication, created_at
)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
patient_id,
int(row["Age"]),
row["Sex"],
json.dumps(row["History"]),
json.dumps(row["Symptoms"]),
json.dumps(row["Medication"]),
datetime.now(),
),
)
conn.commit()
conn.close()
def get_records_by_patient(patient_id):
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
try:
c = conn.cursor()
c.execute(
"""
SELECT
patient_id, age, sex, history, symptoms, medication, created_at
FROM records
WHERE patient_id = ?
""",
(patient_id,),
)
records = c.fetchall()
return [dict(record) for record in records]
finally:
conn.close()