PCM_Report/test_table_columns.py

170 lines
5.1 KiB
Python
Raw Normal View History

2025-12-11 14:32:31 +08:00
#!/usr/bin/env python3
"""
测试表格列修改
"""
import sqlite3
from pathlib import Path
def check_database_schema():
"""检查数据库结构"""
print("检查数据库结构")
print("=" * 40)
try:
db_path = Path(__file__).parent / "experiments.db"
db = sqlite3.connect(str(db_path))
cur = db.cursor()
# 检查表结构
cur.execute("PRAGMA table_info(experiments)")
columns = cur.fetchall()
print("experiments表结构:")
for col in columns:
cid, name, type_name, notnull, default_value, pk = col
nullable = "NOT NULL" if notnull else "NULL"
default = f"DEFAULT {default_value}" if default_value else ""
primary = "PRIMARY KEY" if pk else ""
print(f" {name}: {type_name} {nullable} {default} {primary}")
db.close()
except Exception as e:
print(f"ERROR 检查失败: {e}")
def check_experiment_data():
"""检查实验数据"""
print("\n检查实验数据")
print("=" * 40)
try:
db_path = Path(__file__).parent / "experiments.db"
db = sqlite3.connect(str(db_path))
cur = db.cursor()
# 查询最新的几条记录
cur.execute("""
SELECT id, start_ts, end_ts, work_order_no, executor, remark, created_at
FROM experiments
ORDER BY id DESC
LIMIT 5
""")
results = cur.fetchall()
if results:
print("最新的实验记录:")
for row in results:
exp_id, start_ts, end_ts, work_order_no, executor, remark, created_at = row
print(f" ID: {exp_id}")
print(f" 开始时间: {start_ts or '未开始'}")
print(f" 结束时间: {end_ts or '未结束'}")
print(f" 工单号: {work_order_no or ''}")
print(f" 操作员: {executor or ''}")
print(f" 备注: {remark or ''}")
print(f" 创建时间: {created_at or ''}")
print("-" * 30)
else:
print("没有实验记录")
db.close()
except Exception as e:
print(f"ERROR 查询失败: {e}")
def add_test_data():
"""添加测试数据"""
print("\n添加测试数据")
print("=" * 40)
try:
db_path = Path(__file__).parent / "experiments.db"
db = sqlite3.connect(str(db_path))
cur = db.cursor()
# 添加一条测试记录
import datetime
test_data = {
'start_ts': datetime.datetime.now().isoformat(timespec='seconds'),
'end_ts': None,
'config_json': '{}',
'template_path': '',
'remark': '这是一个测试备注',
'work_order_no': 'TEST001',
'executor': '张三',
'created_at': datetime.datetime.now().isoformat(timespec='seconds')
}
cur.execute("""
INSERT INTO experiments(start_ts, end_ts, config_json, template_path, remark, work_order_no, executor, created_at)
VALUES(?,?,?,?,?,?,?,?)
""", (
test_data['start_ts'],
test_data['end_ts'],
test_data['config_json'],
test_data['template_path'],
test_data['remark'],
test_data['work_order_no'],
test_data['executor'],
test_data['created_at']
))
new_id = cur.lastrowid
db.commit()
db.close()
print(f"OK 添加测试记录 ID: {new_id}")
print(f" 工单号: {test_data['work_order_no']}")
print(f" 操作员: {test_data['executor']}")
print(f" 备注: {test_data['remark']}")
except Exception as e:
print(f"ERROR 添加失败: {e}")
def main():
print("表格列修改测试")
print("=" * 50)
print("修改内容:")
print("1. 新增 executor 字段")
print("2. 表格增加工单号和操作员列")
print("3. 备注不再强制显示工单号")
print("=" * 50)
# 1. 检查数据库结构
check_database_schema()
# 2. 检查现有数据
check_experiment_data()
# 3. 添加测试数据
add_test_data()
# 4. 再次检查数据
check_experiment_data()
print("\n" + "=" * 50)
print("修改总结:")
print("✅ 数据库新增 executor 字段")
print("✅ 表格列调整:")
print(" - 开始时间 (列0)")
print(" - 结束时间 (列1)")
print(" - 工单号 (列2) ← 新增")
print(" - 操作员 (列3) ← 新增")
print(" - 实验备注 (列4)")
print(" - 暂停按钮 (列5)")
print(" - 状态 (列6)")
print(" - 查看看板 (列7)")
print(" - 详情 (列8)")
print(" - 生成报告 (列9)")
print(" - 删除 (列10)")
print("\n现在可以:")
print("1. 重启程序查看新的表格布局")
print("2. 创建新工单测试数据显示")
print("3. 验证备注不再强制显示工单号")
if __name__ == "__main__":
main()