将数据库的prompt换为UNIQUE唯一属性,并且插入语句换成REPLACE INTO

This commit is contained in:
w_xiaolizu
2023-06-06 12:28:02 +08:00
parent 86c8184d20
commit a2c02636fa
2 changed files with 11 additions and 8 deletions

View File

@ -287,8 +287,8 @@ def diff_list(txt='', percent=0.70, switch: list = None, lst: list = None, sp=15
import difflib
count_dict = {}
if not lst:
lst = SqliteHandle('ai_common').get_prompt_value()
lst.update(SqliteHandle(f"ai_private_{hosts}").get_prompt_value())
lst = SqliteHandle('ai_common').get_prompt_value(txt)
lst.update(SqliteHandle(f"ai_private_{hosts}").get_prompt_value(txt))
# diff 数据根据precent系数归类数据
for i in lst:
found = False
@ -378,10 +378,10 @@ def prompt_retrieval(is_all, hosts='', search=False):
if '所有人' in is_all:
for tab in SqliteHandle('ai_common').get_tables():
if tab.startswith('prompt'):
data = SqliteHandle(tab).get_prompt_value()
data = SqliteHandle(tab).get_prompt_value(None)
if data: count_dict.update(data)
elif '个人' in is_all:
data = SqliteHandle(f'prompt_{hosts}').get_prompt_value()
data = SqliteHandle(f'prompt_{hosts}').get_prompt_value(None)
if data: count_dict.update(data)
retrieval = []
if count_dict != {}:

View File

@ -41,7 +41,7 @@ class SqliteHandle:
self.__connect.close()
def create_tab(self):
self.__cursor.execute(f"CREATE TABLE `{self.__table}` ( 'prompt' TEXT, 'result' TEXT)")
self.__cursor.execute(f"CREATE TABLE `{self.__table}` ('prompt' TEXT UNIQUE, 'result' TEXT)")
def get_tables(self):
all_tab = []
@ -50,16 +50,19 @@ class SqliteHandle:
all_tab.append(tab[0])
return all_tab
def get_prompt_value(self):
def get_prompt_value(self, find):
temp_all = {}
result = self.__cursor.execute(f"SELECT prompt, result FROM `{self.__table}`").fetchall()
if find:
result = self.__cursor.execute(f"SELECT prompt, result FROM `{self.__table}` WHERE prompt LIKE '%{find}%'").fetchall()
else:
result = self.__cursor.execute(f"SELECT prompt, result FROM `{self.__table}`").fetchall()
for row in result:
temp_all[row[0]] = row[1]
return temp_all
def inset_prompt(self, prompt: dict):
for key in prompt:
self.__cursor.execute(f"INSERT INTO `{self.__table}` (prompt, result) VALUES (?, ?);", (str(key), str(prompt[key])))
self.__cursor.execute(f"REPLACE INTO `{self.__table}` (prompt, result) VALUES (?, ?);", (str(key), str(prompt[key])))
self.__connect.commit()
def delete_prompt(self):