11
This commit is contained in:
+18
-6
@@ -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)
|
||||
Reference in New Issue
Block a user