forked from JamsheerJabbar/Hackathon_backend_deriv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_actual_schema.py
More file actions
30 lines (23 loc) · 824 Bytes
/
get_actual_schema.py
File metadata and controls
30 lines (23 loc) · 824 Bytes
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
import sqlite3
import json
def get_full_schema():
conn = sqlite3.connect('derivinsightnew.db')
cursor = conn.cursor()
schema = {}
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [r[0] for r in cursor.fetchall()]
for table in tables:
cursor.execute(f"PRAGMA table_info({table})")
columns = [r[1] for r in cursor.fetchall()]
# Get one row to see sample data
cursor.execute(f"SELECT * FROM {table} LIMIT 1")
row = cursor.fetchone()
sample = dict(zip(columns, row)) if row else {}
schema[table] = {
"columns": columns,
"sample": sample
}
conn.close()
return schema
if __name__ == "__main__":
print(json.dumps(get_full_schema(), indent=2))