-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
47 lines (39 loc) · 1.14 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
import json
import mysql.connector
DB_HOST = 'localhost'
DB_NAME = 'chatroom'
DB_USERNAME = 'root'
DB_PASSWORD = '12345678'
def db_conn():
conn = mysql.connector.connect(
host = DB_HOST,
user = DB_USERNAME,
passwd = DB_PASSWORD,
database= DB_NAME
)
return conn
conn = db_conn()
cursor = conn.cursor()
def jsonify(res):
row_headers=[x[0] for x in cursor.description]
json_data=[]
for result in res:
json_data.append(dict(zip(row_headers,result)))
return json_data
def getRooms():
cursor.execute("SELECT id, name FROM rooms")
res = cursor.fetchall()
return jsonify(res)
def insertRoom(name):
sql = "INSERT INTO rooms (name) VALUES (%s)"
cursor.execute(sql, (name,))
conn.commit()
def insertMessage(room_id, username, body):
sql = "INSERT INTO messages (room_id, username, body) VALUES (%s, %s, %s)"
cursor.execute(sql, (room_id, username, body))
conn.commit()
def getRoomMessages(room_id):
sql = "SELECT username, body FROM messages WHERE room_id=%s" % room_id
cursor.execute(sql)
res = cursor.fetchall()
return jsonify(res)