mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-14 06:30:57 +00:00
fix: 数据库操作中字符串转义操作造成多个"/" #20
This commit is contained in:
+19
-14
@@ -2,14 +2,13 @@ import logging
|
|||||||
import time
|
import time
|
||||||
from sqlite3 import Cursor
|
from sqlite3 import Cursor
|
||||||
|
|
||||||
from pymysql.converters import escape_string
|
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
|
||||||
inst = None
|
inst = None
|
||||||
|
|
||||||
|
|
||||||
# 数据库管理
|
# 数据库管理
|
||||||
# 为其他模块提供数据库操作接口
|
# 为其他模块提供数据库操作接口
|
||||||
class DatabaseManager:
|
class DatabaseManager:
|
||||||
@@ -29,9 +28,9 @@ class DatabaseManager:
|
|||||||
# self.conn.isolation_level = None
|
# self.conn.isolation_level = None
|
||||||
self.cursor = self.conn.cursor()
|
self.cursor = self.conn.cursor()
|
||||||
|
|
||||||
def execute(self, sql: str) -> Cursor:
|
def execute(self, *args, **kwargs) -> Cursor:
|
||||||
logging.debug('SQL: {}'.format(sql))
|
# logging.debug('SQL: {}'.format(sql))
|
||||||
c = self.cursor.execute(sql)
|
c = self.cursor.execute(*args, **kwargs)
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return c
|
return c
|
||||||
|
|
||||||
@@ -62,17 +61,23 @@ class DatabaseManager:
|
|||||||
""".format(subject_type, subject_number, create_timestamp))
|
""".format(subject_type, subject_number, create_timestamp))
|
||||||
count = self.cursor.fetchone()[0]
|
count = self.cursor.fetchone()[0]
|
||||||
if count == 0:
|
if count == 0:
|
||||||
self.execute("""
|
|
||||||
|
sql = """
|
||||||
insert into `sessions` (`name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`)
|
insert into `sessions` (`name`, `type`, `number`, `create_timestamp`, `last_interact_timestamp`, `prompt`)
|
||||||
values ('{}', '{}', {}, {}, {}, '{}')
|
values (?, ?, ?, ?, ?, ?)
|
||||||
""".format("{}_{}".format(subject_type, subject_number), subject_type, subject_number, create_timestamp,
|
"""
|
||||||
last_interact_timestamp, escape_string(prompt)))
|
|
||||||
|
self.execute(sql,
|
||||||
|
("{}_{}".format(subject_type, subject_number), subject_type, subject_number, create_timestamp,
|
||||||
|
last_interact_timestamp, prompt))
|
||||||
else:
|
else:
|
||||||
self.execute("""
|
sql = """
|
||||||
update `sessions` set `last_interact_timestamp` = {}, `prompt` = '{}'
|
update `sessions` set `last_interact_timestamp` = ?, `prompt` = ?
|
||||||
where `type` = '{}' and `number` = {} and `create_timestamp` = {}
|
where `type` = ? and `number` = ? and `create_timestamp` = ?
|
||||||
""".format(last_interact_timestamp, escape_string(prompt), subject_type,
|
"""
|
||||||
subject_number, create_timestamp))
|
|
||||||
|
self.execute(sql, (last_interact_timestamp, prompt, subject_type,
|
||||||
|
subject_number, create_timestamp))
|
||||||
|
|
||||||
# 显式关闭一个session
|
# 显式关闭一个session
|
||||||
def explicit_close_session(self, session_name: str, create_timestamp: int):
|
def explicit_close_session(self, session_name: str, create_timestamp: int):
|
||||||
|
|||||||
Reference in New Issue
Block a user