-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
144 lines (129 loc) · 3.8 KB
/
db.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# import sqlite3
# import uuid
#
#
# def get_connection():
# """
# Establish a connection to the database and return a connection object.
# """
# conn = sqlite3.connect("chatbot.db")
# return conn
#
#
# def insert_message(body, message_id):
# """
# Insert a new message into the "messages" table.
# """
# with get_connection() as conn:
# with conn:
# cursor = conn.cursor()
# cursor.execute("INSERT INTO messages (body, message_id) VALUES (?, ?)", (body, message_id))
#
#
# def insert_response(body, message_id):
# """
# Insert a new chatbot response into the "chatbot_responses" table.
# :param body:
# :param message_id:
# :return:
# """
# with get_connection() as conn:
# cursor = conn.cursor()
# cursor.execute("INSERT INTO responses (body, message_id) VALUES (?, ?)", (body, message_id))
#
#
# def get_messages():
# """
# Retrieve all messages from the "messages" table.
# """
# with get_connection() as conn:
# with conn:
# cursor = conn.cursor()
# cursor.execute("SELECT * FROM messages")
# return cursor.fetchall()
#
#
# def get_responses(message_id):
# """
# Retrieve all responses for a given message ID.
# """
# with get_connection() as conn:
# with conn:
# cursor = conn.cursor()
# cursor.execute("SELECT * FROM chatbot_responses WHERE message_id=?", (message_id,))
# return cursor.fetchall()
import sqlite3
import uuid
import logging
logger = logging.getLogger(__name__)
def create_connection():
"""
Creates a connection to the database and returns a connection object.
"""
conn = sqlite3.connect("chatbot.db")
return conn
def insert_message(body, message_id):
"""
Insert a new message into the "messages" table.
"""
if not body or not message_id:
raise ValueError("Both 'body' and 'message_id' are required fields")
conn = create_connection()
try:
cursor = conn.cursor()
cursor.execute("INSERT INTO messages (body, message_id) VALUES (?, ?)", (body, message_id))
conn.commit()
except Exception as e:
logger.error(f"Error inserting message into database: {e}")
raise
finally:
conn.close()
def insert_response(body, message_id):
"""
Insert a new chatbot response into the "chatbot_responses" table.
:param body:
:param message_id:
:return:
"""
if not body or not message_id:
raise ValueError("Both 'body' and 'message_id' are required fields")
conn = create_connection()
try:
cursor = conn.cursor()
cursor.execute("INSERT INTO responses (body, message_id) VALUES (?, ?)", (body, message_id))
conn.commit()
except Exception as e:
logger.error(f"Error inserting response into database: {e}")
raise
finally:
conn.close()
def get_messages():
"""
Retrieve all messages from the "messages" table.
"""
conn = create_connection()
try:
cursor = conn.cursor()
cursor.execute("SELECT * FROM messages")
return cursor.fetchall()
except Exception as e:
logger.error(f"Error retrieving messages from database: {e}")
raise
finally:
conn.close()
def get_responses(message_id):
"""
Retrieve all responses for a given message ID.
"""
if not message_id:
raise ValueError("'message_id' is a required field")
conn = create_connection()
try:
cursor = conn.cursor()
cursor.execute("SELECT * FROM responses WHERE message_id=?", (message_id,))
return cursor.fetchall()
except Exception as e:
logger.error(f"Error retrieving responses from database: {e}")
raise
finally:
conn.close()