Update database.py
Browse files- database.py +25 -15
database.py
CHANGED
@@ -1,19 +1,29 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
Returns:
|
10 |
-
|
11 |
"""
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sqlalchemy import create_engine, MetaData, inspect
|
2 |
+
|
3 |
+
# Initialize database engine (Persistent SQLite)
|
4 |
+
engine = create_engine("sqlite:///database.db")
|
5 |
+
metadata_obj = MetaData()
|
6 |
|
7 |
+
# Function to check existing tables
|
8 |
+
def get_existing_tables():
|
9 |
+
"""
|
10 |
+
Returns a list of existing tables in the database.
|
11 |
|
12 |
Returns:
|
13 |
+
list: List of table names.
|
14 |
"""
|
15 |
+
inspector = inspect(engine)
|
16 |
+
return inspector.get_table_names()
|
17 |
+
|
18 |
+
# Function to initialize database (without forcing a table)
|
19 |
+
def initialize_database():
|
20 |
+
"""
|
21 |
+
Ensures the database starts up without failing if no SQL file is uploaded yet.
|
22 |
+
"""
|
23 |
+
tables = get_existing_tables()
|
24 |
+
if not tables:
|
25 |
+
print("No tables found. Waiting for SQL file upload...")
|
26 |
+
else:
|
27 |
+
print(f"Database initialized with tables: {tables}")
|
28 |
+
|
29 |
+
initialize_database()
|