PCM_Report/check_db_schema.py

95 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""
检查数据库结构
"""
import sqlite3
from pathlib import Path
def check_db_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 create_test_experiment():
"""创建测试实验(包含所有必需字段)"""
print("\n创建测试实验")
print("=" * 40)
try:
db_path = Path(__file__).parent / "experiments.db"
db = sqlite3.connect(str(db_path))
cur = db.cursor()
# 查看现有记录的config_json格式
cur.execute("SELECT config_json FROM experiments LIMIT 1")
result = cur.fetchone()
if result:
sample_config = result[0]
print(f"示例config_json: {sample_config[:100]}...")
else:
sample_config = "{}"
# 创建新记录
import datetime
cur.execute("""
INSERT INTO experiments (work_order_no, remark, created_at, config_json)
VALUES (?, ?, ?, ?)
""", (
"112233",
"测试工单: 112233",
datetime.datetime.now().isoformat(timespec='seconds'),
sample_config or "{}"
))
new_exp_id = cur.lastrowid
db.commit()
db.close()
print(f"OK 创建新实验记录: ID {new_exp_id}")
return new_exp_id
except Exception as e:
print(f"ERROR 创建失败: {e}")
return None
def main():
print("数据库结构检查")
print("=" * 50)
check_db_schema()
exp_id = create_test_experiment()
if exp_id:
print(f"\n成功创建测试实验: {exp_id}")
print("现在可以测试状态变化了")
else:
print("\n创建测试实验失败")
if __name__ == "__main__":
main()