-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_to_mysql_image.py
52 lines (43 loc) · 1.36 KB
/
store_to_mysql_image.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
import json
import mysql.connector
# Load JSON Data
with open("structured_data.json", "r") as json_file:
patient_record = json.load(json_file)
# Database Connection
conn = mysql.connector.connect(
host="localhost",
user="your_user",
password="your_password",
database="hospital_db"
)
cursor = conn.cursor()
# Create Tables (if not exists)
cursor.execute('''
CREATE TABLE IF NOT EXISTS patients (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
dob DATE
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS forms_data (
id INT AUTO_INCREMENT PRIMARY KEY,
patient_id INT,
form_json JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (patient_id) REFERENCES patients(id)
);
''')
# Insert Data into Tables
name = patient_record.get("patient_name", "Unknown")
dob = patient_record.get("dob", "2000-01-01") # Default DOB if not available
# Insert into patients table
cursor.execute("INSERT INTO patients (name, dob) VALUES (%s, %s)", (name, dob))
patient_id = cursor.lastrowid
# Insert JSON into forms_data table
cursor.execute("INSERT INTO forms_data (patient_id, form_json) VALUES (%s, %s)", (patient_id, json.dumps(patient_record)))
# Commit and Close Connection
conn.commit()
cursor.close()
conn.close()
print("Data successfully inserted into MySQL database!")