ytrsoymr commited on
Commit
adaf98c
·
verified ·
1 Parent(s): 9660ca1

Update chatbot/database.py

Browse files
Files changed (1) hide show
  1. chatbot/database.py +10 -13
chatbot/database.py CHANGED
@@ -1,5 +1,5 @@
1
  import sqlite3
2
- from config import DB_PATH # Import database path from config.py
3
 
4
  def create_table():
5
  """Creates the chat history table if it doesn't exist."""
@@ -8,7 +8,7 @@ def create_table():
8
  cursor.execute("""
9
  CREATE TABLE IF NOT EXISTS chat_history (
10
  id INTEGER PRIMARY KEY AUTOINCREMENT,
11
- user_id TEXT,
12
  user_message TEXT,
13
  bot_response TEXT,
14
  timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
@@ -17,27 +17,24 @@ def create_table():
17
  conn.commit()
18
  conn.close()
19
 
20
- def get_chat_history(user_id):
21
- """Fetches chat history for a given user."""
22
  conn = sqlite3.connect(DB_PATH)
23
  cursor = conn.cursor()
24
- cursor.execute("SELECT user_message, bot_response FROM chat_history WHERE user_id=? ORDER BY timestamp",
25
- (str(user_id),)) # Ensure user_id is a string
26
  history = cursor.fetchall()
27
  conn.close()
28
  return history
29
 
30
- def save_chat(user_id, user_message, bot_response):
31
  """Stores a chat message in the database."""
32
  conn = sqlite3.connect(DB_PATH)
33
  cursor = conn.cursor()
34
-
35
- # Ensure all values are stored as strings
36
- cursor.execute("INSERT INTO chat_history (user_id, user_message, bot_response) VALUES (?, ?, ?)",
37
- (str(user_id), str(user_message), str(bot_response)))
38
-
39
  conn.commit()
40
  conn.close()
41
 
42
  # Initialize the database
43
- create_table()
 
1
  import sqlite3
2
+ from config import DB_PATH # Import database path from config.py
3
 
4
  def create_table():
5
  """Creates the chat history table if it doesn't exist."""
 
8
  cursor.execute("""
9
  CREATE TABLE IF NOT EXISTS chat_history (
10
  id INTEGER PRIMARY KEY AUTOINCREMENT,
11
+ session_id TEXT,
12
  user_message TEXT,
13
  bot_response TEXT,
14
  timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
 
17
  conn.commit()
18
  conn.close()
19
 
20
+ def get_chat_history(session_id):
21
+ """Fetches chat history for a given session."""
22
  conn = sqlite3.connect(DB_PATH)
23
  cursor = conn.cursor()
24
+ cursor.execute("SELECT user_message, bot_response FROM chat_history WHERE session_id=? ORDER BY timestamp",
25
+ (session_id,))
26
  history = cursor.fetchall()
27
  conn.close()
28
  return history
29
 
30
+ def save_chat(session_id, user_message, bot_response):
31
  """Stores a chat message in the database."""
32
  conn = sqlite3.connect(DB_PATH)
33
  cursor = conn.cursor()
34
+ cursor.execute("INSERT INTO chat_history (session_id, user_message, bot_response) VALUES (?, ?, ?)",
35
+ (session_id, user_message, bot_response))
 
 
 
36
  conn.commit()
37
  conn.close()
38
 
39
  # Initialize the database
40
+ create_table()