Quazim0t0 commited on
Commit
dddb5bd
·
verified ·
1 Parent(s): e465450

Update database.py

Browse files
Files changed (1) hide show
  1. 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
- if 'data_table' not in metadata_obj.tables:
78
- return None
 
 
 
 
 
 
 
 
 
 
79
 
80
- table = metadata_obj.tables['data_table']
81
- schema = []
82
- for column in table.columns:
83
- col_type = str(column.type).upper()
84
- is_primary = "primary key" if column.primary_key else ""
85
- schema.append(f"- {column.name} ({col_type}) {is_primary}".strip())
86
-
87
- return "\n".join(schema)
 
 
 
 
 
 
 
 
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