fix up db fetch and insert

This commit is contained in:
C committed 2025-10-26 15:48:17 -07:00
1 parent 4dfe50db93
commit 9804b6d673
1 file changed
+12 -12
+12 -12
View File
@@ -3,7 +3,11 @@ from time import time
time = int(time()) # we dont need no stinkin floats time = int(time()) # we dont need no stinkin floats
# db = await aiosqlite.connect("edderkop.db") # type: ignore # db = await aiosqlite.connect("edderkop.db") # type: ignore
async def fetch(sqobj):
async def close(db):
await db.close()
async def fetch(table, sqobj):
""" """
Asynchronously fetch a record from the REVIEW table by sq_object. Asynchronously fetch a record from the REVIEW table by sq_object.
@@ -13,21 +17,17 @@ async def fetch(sqobj):
dict or None: The fetched record as a dictionary, or None if not found. dict or None: The fetched record as a dictionary, or None if not found.
""" """
async with aiosqlite.connect("edderkop.db") as db: # type: ignore async with aiosqlite.connect("edderkop.db") as db: # type: ignore
db.row_factory = aiosqlite.Row # type: ignore async with db.execute(f"SELECT * FROM {table} WHERE sq_object = {sqobj}") as cursor:
async with db.execute( async for row in cursor:
"SELECT * FROM REVIEW WHERE sq_object = ?", (sqobj,) return row
) as cursor:
row = await cursor.fetchone()
if row:
return dict(row)
return None
async def upsert(table: str, data: dict, unique_key: str) -> None: return None
async def upsert(table: str, data: dict, unique_key: str) -> bool:
""" """
Asynchronously insert or update a record in the specified table. Asynchronously insert or update a record in the specified table.
Args: Args:
sq_ib (aiosqlite.Connection): The SQLite database connection.
table (str): The name of the table to upsert into. table (str): The name of the table to upsert into.
data (dict): A dictionary of column-value pairs to insert or update. data (dict): A dictionary of column-value pairs to insert or update.
unique_key (str): The column name that serves as the unique key for upsert. unique_key (str): The column name that serves as the unique key for upsert.
@@ -50,4 +50,4 @@ async def upsert(table: str, data: dict, unique_key: str) -> None:
async with aiosqlite.connect("edderkop.db") as db: # type: ignore async with aiosqlite.connect("edderkop.db") as db: # type: ignore
async with db.execute(sql, tuple(data.values())) as cursor: async with db.execute(sql, tuple(data.values())) as cursor:
await db.commit() await db.commit()
#await db.close() return True