Spaces:
Sleeping
Sleeping
File size: 1,327 Bytes
9c37f64 2aa9473 9c37f64 adaf98c 9c37f64 2aa9473 9c37f64 2aa9473 9c37f64 2aa9473 9c37f64 2aa9473 9c37f64 2aa9473 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import sqlite3
from config import DB_PATH # Import database path from config.py
def create_table():
"""Creates the chat history table if it doesn't exist."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS chat_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
user_message TEXT,
bot_response TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
conn.close()
def get_chat_history(session_id):
"""Fetches chat history for a given session."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("SELECT user_message, bot_response FROM chat_history WHERE session_id=? ORDER BY timestamp",
(session_id,))
history = cursor.fetchall()
conn.close()
return history
def save_chat(session_id, user_message, bot_response):
"""Stores a chat message in the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("INSERT INTO chat_history (session_id, user_message, bot_response) VALUES (?, ?, ?)",
(session_id, user_message, bot_response))
conn.commit()
conn.close()
# Initialize the database
create_table() |