TorqueWrench/backend/init_database.py

49 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
初始化数据库从JSON文件导入工单数据
"""
import json
import os
from database import Database
def import_from_json(db: Database, json_file: str = "work_orders.json"):
"""从JSON文件导入工单数据"""
if not os.path.exists(json_file):
print(f"文件不存在: {json_file}")
return False
try:
with open(json_file, 'r', encoding='utf-8') as f:
orders = json.load(f)
count = 0
for order in orders:
if db.create_work_order(order):
count += 1
print(f"✓ 导入工单: {order.get('trace_id')} - {order.get('process_id')}")
else:
print(f"✗ 导入失败: {order.get('trace_id')} - {order.get('process_id')}")
print(f"\n导入完成,共导入 {count} 个工单")
return True
except Exception as e:
print(f"导入失败: {e}")
return False
if __name__ == '__main__':
print("="*60)
print("初始化数据库")
print("="*60)
db = Database("wrench.db")
# 从JSON文件导入数据
if import_from_json(db):
print("\n数据库初始化成功!")
else:
print("\n数据库初始化失败!")