-
Notifications
You must be signed in to change notification settings - Fork 0
/
yoni_db.py
43 lines (37 loc) · 1.07 KB
/
yoni_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
import sqlite3
DatabasePath = "yoni_box.db"
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def create():
conn = sqlite3.connect(DatabasePath)
c = conn.cursor()
c.execute("CREATE TABLE user(user,passwd)")
c.execute("CREATE TABLE messages(id,user,message)")
conn.commit()
conn.close()
def check_user(user):
conn = sqlite3.connect(DatabasePath)
c = conn.cursor()
# 获取passwd
result = c.execute("SELECT * FROM user WHERE user = '%s'" % user).fetchone()
#关闭连接
conn.close()
return result
def check_message():
conn = sqlite3.connect(DatabasePath)
conn.row_factory = dict_factory
c = conn.cursor()
# 获取投稿
result = c.execute("SELECT * FROM messages").fetchall()
#关闭连接
conn.close()
return result
def write(user, message):
conn = sqlite3.connect(DatabasePath)
c = conn.cursor()
c.execute("INSERT INTO messages (user,message) VALUES (?,?)", (user, message))
conn.commit()
conn.close()