问题:使用 lib.db.get_connection() 返回上下文管理器,导致 AttributeError。 解决方案: - 直接使用 pymysql.connect() 创建连接 - 不依赖 lib.db 模块 - 添加测试插入和查询验证 - 确保 file_path 字段能正确保存和读取 现在可以正常运行: ```bash python force_rebuild.py ``` Coze-Commit-Type: user Coze-User-ID: 3722323274763196 Coze-Conversation-ID: 9894087
153 lines
5.6 KiB
Python
153 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
强制修复脚本:删除并重建表,确保file_path字段正确
|
||
"""
|
||
|
||
import pymysql
|
||
|
||
def force_rebuild():
|
||
"""强制重建表"""
|
||
print("="*60)
|
||
print("强制修复数据库表")
|
||
print("="*60)
|
||
|
||
conn = None
|
||
try:
|
||
# 直接创建连接
|
||
print("\n连接数据库...")
|
||
conn = pymysql.connect(
|
||
host='haoslm2.xicp.net',
|
||
port=10216,
|
||
user='root',
|
||
password='DsideaL147258369',
|
||
database='yltcharge',
|
||
charset='utf8mb4',
|
||
cursorclass=pymysql.cursors.DictCursor
|
||
)
|
||
print("✓ 数据库连接成功")
|
||
|
||
cursor = conn.cursor()
|
||
|
||
# 1. 删除旧表
|
||
print("\n1. 删除旧表...")
|
||
cursor.execute("DROP TABLE IF EXISTS t_daily_report_history")
|
||
cursor.execute("DROP TABLE IF EXISTS t_daily_report_config")
|
||
conn.commit()
|
||
print(" ✓ 旧表已删除")
|
||
|
||
# 2. 重建配置表
|
||
print("\n2. 重建配置表...")
|
||
create_config_table = """
|
||
CREATE TABLE t_daily_report_config (
|
||
id BIGINT NOT NULL COMMENT '配置ID',
|
||
config_name VARCHAR(100) NOT NULL COMMENT '配置名称',
|
||
split_type VARCHAR(20) NOT NULL COMMENT '拆分方式',
|
||
split_value VARCHAR(100) NOT NULL COMMENT '拆分值',
|
||
split_name VARCHAR(200) NULL COMMENT '拆分显示名称',
|
||
selected_fields VARCHAR(3000) NOT NULL COMMENT '选择的字段列表',
|
||
sum_fields VARCHAR(3000) NULL COMMENT '求和字段列表',
|
||
is_active TINYINT NULL COMMENT '是否启用',
|
||
sort_order INT NULL COMMENT '排序顺序',
|
||
create_time DATETIME NULL COMMENT '创建时间',
|
||
update_time DATETIME NULL COMMENT '更新时间'
|
||
)
|
||
UNIQUE KEY(id)
|
||
DISTRIBUTED BY HASH(id) BUCKETS 1
|
||
PROPERTIES("replication_num" = "1")
|
||
"""
|
||
cursor.execute(create_config_table)
|
||
conn.commit()
|
||
print(" ✓ 配置表已重建")
|
||
|
||
# 3. 重建历史表(确保file_path字段正确)
|
||
print("\n3. 重建历史表...")
|
||
create_history_table = """
|
||
CREATE TABLE t_daily_report_history (
|
||
id BIGINT NOT NULL COMMENT '历史ID',
|
||
report_date DATE NOT NULL COMMENT '报表日期',
|
||
start_time DATETIME NOT NULL COMMENT '开始时间',
|
||
end_time DATETIME NOT NULL COMMENT '结束时间',
|
||
split_type VARCHAR(20) NOT NULL COMMENT '拆分方式',
|
||
split_value VARCHAR(100) NOT NULL COMMENT '拆分值',
|
||
split_name VARCHAR(200) NULL COMMENT '拆分显示名称',
|
||
config_id BIGINT NULL COMMENT '配置ID',
|
||
total_orders INT NULL COMMENT '订单总数',
|
||
total_amount DECIMAL(15,2) NULL COMMENT '总金额',
|
||
sum_results VARCHAR(2000) NULL COMMENT '求和结果',
|
||
file_path VARCHAR(500) NULL COMMENT '文件路径',
|
||
status TINYINT NULL COMMENT '状态',
|
||
error_message VARCHAR(1000) NULL COMMENT '错误信息',
|
||
create_time DATETIME NULL COMMENT '创建时间'
|
||
)
|
||
UNIQUE KEY(id)
|
||
DISTRIBUTED BY HASH(id) BUCKETS 1
|
||
PROPERTIES("replication_num" = "1")
|
||
"""
|
||
cursor.execute(create_history_table)
|
||
conn.commit()
|
||
print(" ✓ 历史表已重建")
|
||
|
||
# 4. 验证表结构
|
||
print("\n4. 验证表结构...")
|
||
cursor.execute("SHOW CREATE TABLE t_daily_report_history")
|
||
result = cursor.fetchone()
|
||
if result and 'file_path' in result.get('Create Table', ''):
|
||
print(" ✓ file_path 字段存在")
|
||
else:
|
||
print(" ⚠️ 无法确认 file_path 字段")
|
||
|
||
# 5. 测试插入
|
||
print("\n5. 测试插入数据...")
|
||
cursor.execute("""
|
||
INSERT INTO t_daily_report_history
|
||
(id, report_date, start_time, end_time, split_type, split_value,
|
||
total_orders, total_amount, file_path, status, create_time)
|
||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())
|
||
""", (
|
||
9999999999,
|
||
'2025-01-01',
|
||
'2025-01-01 08:00:00',
|
||
'2025-01-02 08:00:00',
|
||
'test',
|
||
'test',
|
||
0,
|
||
0.00,
|
||
'/reports/test.xlsx',
|
||
1
|
||
))
|
||
conn.commit()
|
||
print(" ✓ 测试数据插入成功")
|
||
|
||
# 6. 查询验证
|
||
cursor.execute("SELECT file_path FROM t_daily_report_history WHERE id = 9999999999")
|
||
result = cursor.fetchone()
|
||
print(f" ✓ 查询结果: file_path = '{result.get('file_path')}'")
|
||
|
||
# 7. 删除测试数据
|
||
cursor.execute("DELETE FROM t_daily_report_history WHERE id = 9999999999")
|
||
conn.commit()
|
||
print(" ✓ 测试数据已清理")
|
||
|
||
print("\n" + "="*60)
|
||
print("✓ 修复完成!")
|
||
print("="*60)
|
||
print("\n下一步:")
|
||
print("1. 重启服务: python app.py")
|
||
print("2. 创建配置")
|
||
print("3. 生成日报")
|
||
print("4. 查看历史记录(应该有下载按钮)")
|
||
|
||
except Exception as e:
|
||
print(f"\n❌ 修复失败: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
finally:
|
||
if conn:
|
||
conn.close()
|
||
|
||
input("\n按回车键退出...")
|
||
|
||
if __name__ == "__main__":
|
||
force_rebuild()
|