corp20250923
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
修复update_query_result函数中的问题
|
||||
主要解决以下问题:
|
||||
1. 受影响行数的获取逻辑不准确
|
||||
2. 添加更详细的错误处理
|
||||
3. 确保数据库事务的正确性
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
# 配置日志
|
||||
handlers = [logging.FileHandler('fix_log.log'), logging.StreamHandler()]
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||
handlers=handlers
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def main():
|
||||
"""
|
||||
主函数:执行修复操作
|
||||
"""
|
||||
logger.info("开始修复update_query_result函数")
|
||||
|
||||
try:
|
||||
# 读取当前api.py文件内容
|
||||
with open('api.py', 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 定义需要替换的代码块和新代码块
|
||||
old_code = """ # 获取受影响的行数
|
||||
affected_rows = cursor.rowcount
|
||||
|
||||
# 关闭数据库连接
|
||||
close_db_connection(conn)
|
||||
|
||||
# 检查是否有记录被更新
|
||||
if affected_rows == 0:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': '未找到id为 {} 的记录'.format(query_id),
|
||||
'data': None
|
||||
}), 404"""
|
||||
|
||||
new_code = """ # 检查query_history表是否有记录被更新
|
||||
# 重新查询来确认更新是否成功
|
||||
cursor.execute("SELECT 1 FROM query_history WHERE id = ?", (query_id,))
|
||||
query_exists = cursor.fetchone() is not None
|
||||
|
||||
# 关闭数据库连接
|
||||
close_db_connection(conn)
|
||||
|
||||
# 检查是否存在该记录
|
||||
if not query_exists:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': '未找到id为 {} 的记录'.format(query_id),
|
||||
'data': None
|
||||
}), 404"""
|
||||
|
||||
# 执行替换
|
||||
if old_code in content:
|
||||
new_content = content.replace(old_code, new_code)
|
||||
|
||||
# 写入修复后的代码
|
||||
with open('api.py', 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
|
||||
logger.info("成功修复update_query_result函数,解决了受影响行数的获取问题")
|
||||
|
||||
# 添加更详细的异常处理
|
||||
add_detailed_exception_handling()
|
||||
|
||||
else:
|
||||
logger.warning("未找到需要修复的代码块,可能已经被修改过")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"修复过程中发生错误: {str(e)}")
|
||||
|
||||
def add_detailed_exception_handling():
|
||||
"""
|
||||
为update_query_result函数添加更详细的异常处理
|
||||
"""
|
||||
try:
|
||||
# 读取当前api.py文件内容
|
||||
with open('api.py', 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 定义需要替换的代码块和新代码块
|
||||
old_except_block = """ except Exception as e:
|
||||
# 确保关闭数据库连接
|
||||
if 'conn' in locals():
|
||||
close_db_connection(conn)
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e),
|
||||
'data': None
|
||||
}), 500"""
|
||||
|
||||
new_except_block = """ except sqlite3.IntegrityError as e:
|
||||
# 确保关闭数据库连接
|
||||
if 'conn' in locals():
|
||||
close_db_connection(conn)
|
||||
logger.error(f"数据库完整性错误: {str(e)}")
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'数据库完整性错误: {str(e)}',
|
||||
'data': None
|
||||
}), 500
|
||||
except sqlite3.Error as e:
|
||||
# 确保关闭数据库连接
|
||||
if 'conn' in locals():
|
||||
close_db_connection(conn)
|
||||
logger.error(f"数据库错误: {str(e)}")
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'数据库错误: {str(e)}',
|
||||
'data': None
|
||||
}), 500
|
||||
except Exception as e:
|
||||
# 确保关闭数据库连接
|
||||
if 'conn' in locals():
|
||||
close_db_connection(conn)
|
||||
logger.error(f"服务器错误: {str(e)}")
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'服务器错误: {str(e)}',
|
||||
'data': None
|
||||
}), 500"""
|
||||
|
||||
# 执行替换
|
||||
if old_except_block in content:
|
||||
new_content = content.replace(old_except_block, new_except_block)
|
||||
|
||||
# 同时添加sqlite3导入
|
||||
if 'import sqlite3' not in content:
|
||||
import_line = "import sqlite3\n"
|
||||
new_content = import_line + new_content
|
||||
logger.info("成功添加sqlite3导入")
|
||||
|
||||
# 写入修复后的代码
|
||||
with open('api.py', 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
|
||||
logger.info("成功为update_query_result函数添加了详细的异常处理")
|
||||
else:
|
||||
logger.warning("未找到需要修复的异常处理代码块")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"添加详细异常处理时发生错误: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user