2026-10-28 11:14:17 +08:00

26 lines
685 B
Python

from dbutils.pooled_db import PooledDB
import pymysql
from config import DB_CONFIG
_pool = None
def get_pool():
global _pool
if _pool is None:
_pool = PooledDB(
creator=pymysql, maxconnections=20, mincached=2, blocking=True,
**DB_CONFIG, cursorclass=pymysql.cursors.DictCursor
)
return _pool
def query(sql, params=None):
conn = get_pool().connection()
try:
with conn.cursor() as cur:
cur.execute(sql, params or ())
if sql.strip().upper().startswith("SELECT"):
return cur.fetchall()
conn.commit()
return cur.lastrowid
finally:
conn.close()