-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
41 lines (35 loc) · 1.09 KB
/
database.py
File metadata and controls
41 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import aiosqlite
import os
# Ensure data directory exists
os.makedirs("./data", exist_ok=True)
async def connection():
"""
Create a new connection each time, explicitly returning the raw connection.
This avoids any thread reuse issues with aiosqlite.
"""
conn = await aiosqlite.connect("./data/main.db", timeout=30.0)
return conn
async def execute(query: str, *args, **kwargs):
"""
Execute a query and commit the changes.
Creates a new connection for each call to avoid thread reuse issues.
"""
conn = await connection()
try:
await conn.execute(query, *args, **kwargs)
await conn.commit()
finally:
await conn.close()
async def execute_fetch(query: str, *args, **kwargs):
"""
Execute a query and return the results.
Creates a new connection for each call to avoid thread reuse issues.
"""
conn = await connection()
try:
cursor = await conn.execute(query, *args, **kwargs)
rows = await cursor.fetchall()
await cursor.close()
return rows
finally:
await conn.close()