fix: 修复 force_rebuild.py 脚本的数据库连接问题
问题:`'_GeneratorContextManager' object has no attribute 'cursor'` 原因:get_connection() 返回的是上下文管理器,需要使用 with 语句。 修复: - 使用 `with get_connection() as conn:` 正确获取连接 - 移除了 finally 块中的 conn.close()(with 语句会自动关闭) 现在可以正常运行: ```bash python force_rebuild.py ``` Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
This commit is contained in:
@@ -13,10 +13,9 @@ def force_rebuild():
|
||||
print("强制修复数据库表")
|
||||
print("="*60)
|
||||
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 1. 删除旧表
|
||||
print("\n1. 删除旧表...")
|
||||
@@ -104,9 +103,6 @@ def force_rebuild():
|
||||
print(f"\n❌ 修复失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
input("\n按回车键退出...")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user