-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
54 lines (45 loc) · 1.41 KB
/
database.py
File metadata and controls
54 lines (45 loc) · 1.41 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
42
43
44
45
46
47
48
49
50
51
52
53
54
import asyncio
import aiosqlite
import os
from typing import Optional
# Ensure data directory exists
os.makedirs("./data", exist_ok=True)
_conn: Optional[aiosqlite.Connection] = None
_conn_lock = asyncio.Lock()
_conn_use_lock = asyncio.Lock()
async def connection() -> aiosqlite.Connection:
"""
复用单个连接,避免频繁创建开销。
"""
global _conn
if _conn is None:
async with _conn_lock:
if _conn is None:
_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.
"""
async with _conn_use_lock:
conn = await connection()
await conn.execute(query, *args, **kwargs)
await conn.commit()
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.
"""
async with _conn_use_lock:
conn = await connection()
cursor = await conn.execute(query, *args, **kwargs)
rows = await cursor.fetchall()
await cursor.close()
return rows
async def close() -> None:
"""关闭数据库连接"""
global _conn
if _conn is not None:
await _conn.close()
_conn = None