This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
98 lines (79 loc) · 2.75 KB
/
db.py
File metadata and controls
98 lines (79 loc) · 2.75 KB
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
import sqlite3
import config
import os
import weakrefdict
db_path = config.IM_CFG_PATH+os.sep+"session"+os.sep+"user"+os.sep+"user.db"
def init(db_path: str, server: str, username: str, nickname: str):
conn = sqlite3.connect(db_path)
conn.execute("CREATE TABLE info (k TEXT PRIMARY KEY,v TEXT);")
conn.execute("INSERT INTO info VALUES ('server', ?);", (server,))
conn.execute("INSERT INTO info VALUES ('username', ?);", (username,))
conn.execute("INSERT INTO info VALUES ('nickname', ?);", (nickname,))
conn.execute("INSERT INTO info VALUES ('session', ?);", ("",))
conn.execute("""CREATE TABLE history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
msgid INT,
groupid INT,
nickname TEXT,
userid TEXT,
self BOOLEAN,
msg TEXT,
at_list TEXT,
type TEXT
);""")
conn.execute("""CREATE TABLE drafts (
groupid INTEGER PRIMARY KEY,
msg TEXT
);""")
conn.commit()
conn.close()
def get_info(key: str):
conn = sqlite3.connect(db_path)
c = conn.execute("SELECT v FROM info WHERE k = ?;", (key,))
return c.fetchone()[0]
def set_info(key: str, value: str):
conn = sqlite3.connect(db_path)
conn.execute("UPDATE info SET v = ? WHERE k = ?;", (value, key))
conn.commit()
conn.close()
def init_info(key: str, value: str):
conn = sqlite3.connect(db_path)
conn.execute(
"INSERT OR IGNORE INTO info (k, v) VALUES (?, ?);", (key, value))
conn.commit()
conn.close()
def load_msg_history(gid: int):
global conn
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 执行查询
cursor.execute("SELECT * FROM history WHERE groupid = ?;", (gid,))
# 获取所有匹配的记录
records = cursor.fetchall()
return [weakrefdict.WeakReferencableDict({
"from": i[3],
"pos": ("right" if i[5] else "left"),
"msg": i[6],
"actions": {},
"userid": i[4],
"id": i[1]
}) for i in records]
def save_msg_history(gid: int, msg: weakrefdict.WeakReferencableDict):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("INSERT INTO history VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?);", (
msg["id"], gid, msg["from"], msg["userid"], msg["pos"] == "right", msg["msg"], msg.get(
"at_list", ""), "markdown"
))
conn.commit()
conn.close()
return cursor.lastrowid
def replace_msg_history(msgid: str, msg: weakrefdict.WeakReferencableDict):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 刷新所有项目
cursor.execute(
"UPDATE history SET msg = ?,msgid = ?,at_list = ? WHERE id = ?;", (msg["msg"], msg["id"], msg.get(
"at_list", ""), msgid))
conn.commit()
conn.close()