This commit is contained in:
qsc
2025-12-05 12:54:25 +08:00
parent 34b8f7267d
commit 2ebf83680b
5 changed files with 88 additions and 37 deletions
+18 -6
View File
@@ -17,26 +17,26 @@ class AsyncSQLite:
async def fetch_one(self, sql: str, params=None):
await self.init()
async with self.conn.execute(sql, params or ()) as cursor:
async with self.conn.execute(sql, params or ()) as cursor: # type: ignore
row = await cursor.fetchone()
return dict(row) if row else None
async def fetch_all(self, sql: str, params=None):
await self.init()
async with self.conn.execute(sql, params or ()) as cursor:
async with self.conn.execute(sql, params or ()) as cursor: # type: ignore
rows = await cursor.fetchall()
return [dict(r) for r in rows]
async def execute(self, sql: str, params=None):
await self.init()
async with self.conn.execute(sql, params or ()):
await self.conn.commit()
async with self.conn.execute(sql, params or ()): # type: ignore
await self.conn.commit() # type: ignore
return True
async def executemany(self, sql: str, params_list):
await self.init()
await self.conn.executemany(sql, params_list)
await self.conn.commit()
await self.conn.executemany(sql, params_list) # type: ignore
await self.conn.commit() # type: ignore
return True
async def insert_record(self, table: str, data: dict):
@@ -56,3 +56,15 @@ class AsyncSQLite:
where_clause = " AND ".join(f"`{k}`=?" for k in where.keys())
sql = f"DELETE FROM `{table}` WHERE {where_clause}"
return await self.execute(sql, tuple(where.values()))
async def clear_table(self, table: str):
"""
清空指定表中的所有记录。
Args:
table: 要清空的表名。
Returns:
如果操作成功则返回 True。
"""
sql = f"DELETE FROM `{table}`"
# 使用 execute 函数执行 DELETE 语句
return await self.execute(sql)
+10 -10
View File
@@ -4,11 +4,11 @@ from typing import Dict, Any, Optional, List, Union
from astrbot.api import logger
from .request import APIClient
from .async_mysql import AsyncMySQL
from .aiosqlite import AsyncSQLite
from .function_basic import load_template,flatten_field,extract_fields,gold_to_string
class JX3Service:
def __init__(self, api_config,db: AsyncMySQL ):
def __init__(self, api_config,db: AsyncSQLite ):
self._api = APIClient()
self._db = db
self._api_config = api_config
@@ -82,7 +82,7 @@ class JX3Service:
data: Optional[Dict[str, Any]] = await self._base_request(
"jx3_richang", "GET", params=params
)
logger.info(f"richang 接口返回数据: {data}")
if not data:
return_data["msg"] = "获取接口信息失败"
return return_data
@@ -101,10 +101,10 @@ class JX3Service:
)
# 安全地处理列表索引
luck = data.get('luck', [None, None, None])
luck_msg = f"[宠物福缘]\n{luck[0] or ''},{luck[1] or ''},{luck[2] or ''}\n"
card = data.get('card', [None, None, None])
card_msg = f"[家园声望·加倍道具]\n{card[0] or ''},{card[1] or ''},{card[2] or ''}\n"
luck = data.get('luck', [])
luck_msg = f"[宠物福缘]\n{', '.join(luck)}\n"
card = data.get('card', [])
card_msg = f"[家园声望·加倍道具]\n{', '.join(card)}\n"
team = data.get('team', [None, None, None])
team_msg = f"[武林通鉴·公共任务]\n{team[0] or ''}\n[武林通鉴·团队秘境]\n{team[2] or ''}\n"
@@ -335,7 +335,7 @@ class JX3Service:
sql = """
INSERT INTO searchdata
(typeName, name, showName, picUrl, searchId, searchDescType)
VALUES (%s, %s, %s, %s, %s, %s)
VALUES (?, ?, ?, ?, ?, ?)
"""
# 增强数据提取的安全性
values_list = [
@@ -352,7 +352,7 @@ class JX3Service:
# 插入数据
try:
await self._db.truncate_table("searchdata")
await self._db.clear_table("searchdata")
await self._db.executemany(sql, values_list)
except Exception as e:
logger.error(f"数据插入失败: {e}")
@@ -382,7 +382,7 @@ class JX3Service:
# 进一步从 DB 查找 ID
try:
sql = "SELECT searchId FROM searchdata WHERE showName=%s"
sql = "SELECT searchId FROM searchdata WHERE showName=?"
sqldata = await self._db.fetch_one(sql, (showName,))
searchId = sqldata["searchId"] if sqldata else None
except Exception as e: