feat:增加存储用户对话记录
This commit is contained in:
54
services/db_service.py
Normal file
54
services/db_service.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
|
||||
def init_db():
|
||||
conn = sqlite3.connect(os.getenv("SQLITE_DB_PATH"))
|
||||
|
||||
# 业务表:会话
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS session (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
thread_id TEXT NOT NULL UNIQUE,
|
||||
username TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
created_time DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def exists_session(thread_id: str, username: str) -> bool:
|
||||
conn = sqlite3.connect(os.getenv("SQLITE_DB_PATH"))
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT 1
|
||||
FROM session
|
||||
WHERE thread_id = ? AND username = ?
|
||||
LIMIT 1
|
||||
""",
|
||||
(thread_id, username)
|
||||
)
|
||||
|
||||
result = cursor.fetchone()
|
||||
conn.close()
|
||||
|
||||
return result is not None
|
||||
|
||||
|
||||
def save_session(thread_id: str, username: str, title: str):
|
||||
conn = sqlite3.connect(os.getenv("SQLITE_DB_PATH"))
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR REPLACE INTO session
|
||||
(thread_id, username, title)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(thread_id, username, title)
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user