49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
#!/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数据库初始化失败!")
|
||
|