Update init_db.py
Browse files- init_db.py +34 -13
init_db.py
CHANGED
|
@@ -1,17 +1,38 @@
|
|
|
|
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 10 |
-
timestamp TEXT,
|
| 11 |
-
user TEXT,
|
| 12 |
-
bot TEXT
|
| 13 |
-
)
|
| 14 |
-
''')
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
| 1 |
+
# 📄 File: init_db.py
|
| 2 |
+
|
| 3 |
import sqlite3
|
| 4 |
|
| 5 |
+
# Name of the database file
|
| 6 |
+
DB_FILE = "chat_history.db"
|
| 7 |
+
|
| 8 |
+
def init_db():
|
| 9 |
+
# Connect to the database (creates file if it doesn't exist)
|
| 10 |
+
conn = sqlite3.connect(DB_FILE)
|
| 11 |
+
c = conn.cursor()
|
| 12 |
+
|
| 13 |
+
# ✅ Create 'chat' table: stores individual messages in a session
|
| 14 |
+
c.execute('''
|
| 15 |
+
CREATE TABLE IF NOT EXISTS chat (
|
| 16 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 17 |
+
session_id TEXT NOT NULL,
|
| 18 |
+
timestamp TEXT NOT NULL,
|
| 19 |
+
user TEXT,
|
| 20 |
+
bot TEXT
|
| 21 |
+
)
|
| 22 |
+
''')
|
| 23 |
+
|
| 24 |
+
# ✅ Create 'sessions' table: stores session metadata
|
| 25 |
+
c.execute('''
|
| 26 |
+
CREATE TABLE IF NOT EXISTS sessions (
|
| 27 |
+
session_id TEXT PRIMARY KEY,
|
| 28 |
+
created_at TEXT NOT NULL
|
| 29 |
+
)
|
| 30 |
+
''')
|
| 31 |
|
| 32 |
+
conn.commit()
|
| 33 |
+
conn.close()
|
| 34 |
+
print("✅ Database initialized successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Run the function if this script is executed directly
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
init_db()
|