big sexy sqlite db
This commit is contained in:
1 parent
5691cebc11
commit
90ca36f426
1 file changed
+53
@@ -0,0 +1,53 @@
|
||||
import aiosqlite
|
||||
from time import time
|
||||
time = int(time()) # we dont need no stinkin floats
|
||||
|
||||
# db = await aiosqlite.connect("edderkop.db") # type: ignore
|
||||
async def fetch(sqobj):
|
||||
"""
|
||||
Asynchronously fetch a record from the REVIEW table by sq_object.
|
||||
|
||||
Args:
|
||||
sqobj (str): The sq_object identifier to search for.
|
||||
Returns:
|
||||
dict or None: The fetched record as a dictionary, or None if not found.
|
||||
"""
|
||||
async with aiosqlite.connect("edderkop.db") as db: # type: ignore
|
||||
db.row_factory = aiosqlite.Row # type: ignore
|
||||
async with db.execute(
|
||||
"SELECT * FROM REVIEW WHERE sq_object = ?", (sqobj,)
|
||||
) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
if row:
|
||||
return dict(row)
|
||||
return None
|
||||
|
||||
async def upsert(table: str, data: dict, unique_key: str) -> None:
|
||||
"""
|
||||
Asynchronously insert or update a record in the specified table.
|
||||
|
||||
Args:
|
||||
sq_ib (aiosqlite.Connection): The SQLite database connection.
|
||||
table (str): The name of the table to upsert into.
|
||||
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.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
print(f"Upserting into {table}: {data}")
|
||||
|
||||
columns = ', '.join(data.keys())
|
||||
placeholders = ', '.join('?' * len(data))
|
||||
updates = ', '.join(f"{col}=excluded.{col}" for col in data.keys() if col != unique_key)
|
||||
|
||||
sql = f"""
|
||||
INSERT INTO {table} ({columns})
|
||||
VALUES ({placeholders})
|
||||
ON CONFLICT({unique_key}) DO UPDATE SET
|
||||
{updates};
|
||||
"""
|
||||
async with aiosqlite.connect("edderkop.db") as db: # type: ignore
|
||||
async with db.execute(sql, tuple(data.values())) as cursor:
|
||||
await db.commit()
|
||||
#await db.close()
|
||||
Reference in new issue
Block a user