forked from harsh-jos/Streamlit-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
57 lines (51 loc) · 1.65 KB
/
database.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sqlite3
def init_db():
conn = sqlite3.connect('chatbot.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password TEXT NOT NULL
)
''')
c.execute('''
CREATE TABLE IF NOT EXISTS chat_history (
username TEXT,
history TEXT,
FOREIGN KEY(username) REFERENCES users(username)
)
''')
conn.commit()
conn.close()
def register_user(username, password):
conn = sqlite3.connect('chatbot.db')
c = conn.cursor()
c.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, password))
conn.commit()
conn.close()
def validate_user(username, password):
conn = sqlite3.connect('chatbot.db')
c = conn.cursor()
c.execute('SELECT * FROM users WHERE username = ? AND password = ?', (username, password))
user = c.fetchone()
conn.close()
return user is not None
def get_chat_history(username):
conn = sqlite3.connect('chatbot.db')
c = conn.cursor()
c.execute('SELECT history FROM chat_history WHERE username = ?', (username,))
row = c.fetchone()
conn.close()
return row[0] if row else ""
def save_chat_history(username, history):
conn = sqlite3.connect('chatbot.db')
c = conn.cursor()
c.execute('INSERT OR REPLACE INTO chat_history (username, history) VALUES (?, ?)', (username, history))
conn.commit()
conn.close()
def clear_chat_history(username):
conn = sqlite3.connect('chatbot.db')
c = conn.cursor()
c.execute('DELETE FROM chat_history WHERE username = ?', (username,))
conn.commit()
conn.close()