import pymysql for db in ("pms_dev", "pms"): c = pymysql.connect(host="10.8.0.252", user="root", password="Hbhyg731024@", database=db) cur = c.cursor() out = [] checks = [ ("charge_orders WHERE battery_id LIKE 'VERIFY-%'", "charge_orders(tmp)"), ("device_logs WHERE content LIKE '%VERIFY-%'", "device_logs(tmp)"), ("download_tasks", "download_tasks(全部)"), ("users WHERE name LIKE 'VERIFY-%' OR phone LIKE '1990000%'", "users(tmp)"), ] for where, label in checks: cur.execute("SELECT COUNT(*) FROM %s" % where) out.append("%s=%d" % (label, cur.fetchone()[0])) print(" ", db, ", ".join(out)) c.close()