修复求和数据统计的问题
This commit is contained in:
95
lib/db.py
95
lib/db.py
@@ -18,8 +18,8 @@ def get_db_config():
|
||||
'cursorclass': pymysql.cursors.DictCursor,
|
||||
'autocommit': True,
|
||||
'connect_timeout': 30,
|
||||
'read_timeout': 60,
|
||||
'write_timeout': 60
|
||||
'read_timeout': 120,
|
||||
'write_timeout': 120
|
||||
}
|
||||
|
||||
DB_CONFIG = get_db_config()
|
||||
@@ -41,39 +41,72 @@ def get_connection():
|
||||
pass
|
||||
|
||||
|
||||
def execute_query(sql, params=None):
|
||||
def execute_query(sql, params=None, retry=2):
|
||||
"""执行查询并返回结果"""
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute(sql, params)
|
||||
result = cursor.fetchall()
|
||||
return result
|
||||
except Exception as e:
|
||||
log_error(f'查询失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
for attempt in range(retry + 1):
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute(sql, params)
|
||||
result = cursor.fetchall()
|
||||
if attempt > 0:
|
||||
log_info(f'[数据库] 查询重试成功,第{attempt+1}次尝试', 'db')
|
||||
return result
|
||||
except pymysql.err.OperationalError as e:
|
||||
if attempt < retry and (e.args[0] == 2013 or e.args[0] == 2006):
|
||||
log_warning(f'[数据库] 查询连接断开,正在重试(第{attempt+1}次): {e}', 'db')
|
||||
import time
|
||||
time.sleep(1)
|
||||
continue
|
||||
log_error(f'查询失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
except Exception as e:
|
||||
log_error(f'查询失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
|
||||
|
||||
def execute_update(sql, params=None):
|
||||
def execute_update(sql, params=None, retry=2):
|
||||
"""执行更新操作"""
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
affected = cursor.execute(sql, params)
|
||||
return cursor.rowcount
|
||||
except Exception as e:
|
||||
log_error(f'更新失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
for attempt in range(retry + 1):
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
affected = cursor.execute(sql, params)
|
||||
if attempt > 0:
|
||||
log_info(f'[数据库] 更新重试成功,第{attempt+1}次尝试', 'db')
|
||||
return cursor.rowcount
|
||||
except pymysql.err.OperationalError as e:
|
||||
if attempt < retry and (e.args[0] == 2013 or e.args[0] == 2006):
|
||||
log_warning(f'[数据库] 更新连接断开,正在重试(第{attempt+1}次): {e}', 'db')
|
||||
import time
|
||||
time.sleep(1)
|
||||
continue
|
||||
log_error(f'更新失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
except Exception as e:
|
||||
log_error(f'更新失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
|
||||
|
||||
def execute_insert(sql, params=None):
|
||||
def execute_insert(sql, params=None, retry=2):
|
||||
"""执行插入操作并返回插入ID"""
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute(sql, params)
|
||||
last_id = cursor.lastrowid
|
||||
return last_id
|
||||
except Exception as e:
|
||||
log_error(f'插入失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
for attempt in range(retry + 1):
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute(sql, params)
|
||||
last_id = cursor.lastrowid
|
||||
if attempt > 0:
|
||||
log_info(f'[数据库] 插入重试成功,第{attempt+1}次尝试', 'db')
|
||||
return last_id
|
||||
except pymysql.err.OperationalError as e:
|
||||
if attempt < retry and (e.args[0] == 2013 or e.args[0] == 2006):
|
||||
log_warning(f'[数据库] 插入连接断开,正在重试(第{attempt+1}次): {e}', 'db')
|
||||
import time
|
||||
time.sleep(1)
|
||||
continue
|
||||
log_error(f'插入失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
except Exception as e:
|
||||
log_error(f'插入失败: {e}\nSQL: {sql}\nParams: {params}', 'db', exc_info=True)
|
||||
raise
|
||||
Reference in New Issue
Block a user