import pymysql import random conn = pymysql.connect(host='10.8.0.252', user='root', password='Hbhyg731024@', database='pms') cur = conn.cursor() # 添加ICCID字段 try: cur.execute("ALTER TABLE cabinets ADD COLUMN iccid VARCHAR(20) DEFAULT NULL COMMENT 'SIM卡ICCID' AFTER imei") print("✓ ICCID字段添加成功") except: print("✓ ICCID字段已存在") # 更新IMEI和ICCID为正确的格式 cur.execute("SELECT id FROM cabinets ORDER BY id") cabinet_ids = [row[0] for row in cur.fetchall()] for i, cab_id in enumerate(cabinet_ids): # IMEI: 15位数字,86开头(中国TAC) imei = f"86{random.randint(10000000000000, 99999999999999)}"[:15] # ICCID: 20位数字,898600开头(中国移动) iccid = f"898600{random.randint(100000000000000000, 999999999999999999)}"[:20] cur.execute(f"UPDATE cabinets SET imei='{imei}', iccid='{iccid}' WHERE id={cab_id}") conn.commit() print(f"✓ 更新了 {len(cabinet_ids)} 个柜子的IMEI和ICCID") # 验证 cur.execute("SELECT imei, iccid FROM cabinets LIMIT 3") for row in cur.fetchall(): print(f" IMEI: {row[0]} ({len(row[0])}位), ICCID: {row[1]} ({len(row[1])}位)") conn.close()