Spaces:
Sleeping
Sleeping
Update database.py
Browse files- database.py +27 -10
database.py
CHANGED
@@ -74,14 +74,31 @@ def get_table_schema():
|
|
74 |
"""
|
75 |
Returns the current table schema as a string.
|
76 |
"""
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
"""
|
75 |
Returns the current table schema as a string.
|
76 |
"""
|
77 |
+
try:
|
78 |
+
# Get list of tables
|
79 |
+
with engine.connect() as con:
|
80 |
+
tables = con.execute(text(
|
81 |
+
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
|
82 |
+
)).fetchall()
|
83 |
+
|
84 |
+
if not tables:
|
85 |
+
return None
|
86 |
+
|
87 |
+
# Use the first table found
|
88 |
+
table_name = tables[0][0]
|
89 |
|
90 |
+
# Get column information
|
91 |
+
with engine.connect() as con:
|
92 |
+
columns = con.execute(text(f"PRAGMA table_info({table_name})")).fetchall()
|
93 |
+
|
94 |
+
schema = []
|
95 |
+
for col in columns:
|
96 |
+
col_name = col[1]
|
97 |
+
col_type = col[2]
|
98 |
+
is_primary = "primary key" if col[5] == 1 else ""
|
99 |
+
schema.append(f"- {col_name} ({col_type}) {is_primary}".strip())
|
100 |
+
|
101 |
+
return "\n".join(schema)
|
102 |
+
|
103 |
+
except Exception as e:
|
104 |
+
return None
|