| # π File: init_db.py | |
| import sqlite3 | |
| # Name of the database file | |
| DB_FILE = "chat_history.db" | |
| def init_db(): | |
| # Connect to the database (creates file if it doesn't exist) | |
| conn = sqlite3.connect(DB_FILE) | |
| c = conn.cursor() | |
| # β Create 'chat' table: stores individual messages in a session | |
| c.execute(''' | |
| CREATE TABLE IF NOT EXISTS chat ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| session_id TEXT NOT NULL, | |
| timestamp TEXT NOT NULL, | |
| user TEXT, | |
| bot TEXT | |
| ) | |
| ''') | |
| # β Create 'sessions' table: stores session metadata | |
| c.execute(''' | |
| CREATE TABLE IF NOT EXISTS sessions ( | |
| session_id TEXT PRIMARY KEY, | |
| created_at TEXT NOT NULL | |
| ) | |
| ''') | |
| conn.commit() | |
| conn.close() | |
| print("β Database initialized successfully.") | |
| # Run the function if this script is executed directly | |
| if __name__ == "__main__": | |
| init_db() |