import sqlite3 import sys DB_PATH = "production.db" def query_product(product_id: str): conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute("SELECT Id, PointName, FieldUnit FROM MeasurementPoints ORDER BY Id") points = cursor.fetchall() if not points: print("[ERROR] 数据库中没有检测点配置") return print(f"\n{'='*50}") print(f" 产品追溯: {product_id}") print(f"{'='*50}\n") tables_exist = False has_data = False for pid, pname, unit in points: table = f"MPData_P{pid}_2026_06_08" cursor.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table,) ) if not cursor.fetchone(): continue tables_exist = True cursor.execute( f"""SELECT RecordTime, IsQualified, InspectionCount, UpperLimit, LowerLimit, MeasuredValue FROM {table} WHERE ProductID = ?""", (product_id,), ) row = cursor.fetchone() if not row: continue has_data = True unit_str = f" ({unit})" if unit else "" print(f" ┌─ {pname}{unit_str}") print(f" │ 检测时间: {row[0]}") print(f" │ 是否合格: {'✓ 合格' if row[1] else '✗ 不合格'}") cnt = row[2] cnt_str = "首次检测" if cnt == 1 else f"第 {cnt} 次检测 (返修)" print(f" │ 检测次数: {cnt} ({cnt_str})") print(f" │ 测量上限: {row[3]}") print(f" │ 测量下限: {row[4]}") print(f" │ 实测值: {row[5]}") print(f" └─────────────────────\n") if not tables_exist: print(" [提示] 数据表尚未创建,请先运行 db_init.py\n") elif not has_data: print(" [提示] 该产品暂无数据记录\n") conn.close() if __name__ == "__main__": product_id = sys.argv[1] if len(sys.argv) > 1 else "PROD-002" query_product(product_id)