backup_tables.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # -*- coding: utf-8 -*-
  2. """备份/查看与本批写接口相关的表(store_role / store_user / address / page / 两张关联表)。"""
  3. import json
  4. import os
  5. import re
  6. import pymysql
  7. YML = r"C:\Users\Administrator\WorkBuddy\2026-10-17-12-27-10\java-rewrite\yoshop-java\src\main\resources\application.yml"
  8. text = open(YML, encoding="utf-8").read()
  9. pwd = re.findall(r"password:\s*(.+)", text)[0].strip().strip('"')
  10. conn = pymysql.connect(host="192.168.1.177", port=3306, user="root", password=pwd,
  11. database="shop", charset="utf8mb4")
  12. cur = conn.cursor()
  13. os.makedirs("backup", exist_ok=True)
  14. TABLES = ["yoshop_store_role", "yoshop_store_user", "yoshop_store_address",
  15. "yoshop_page", "yoshop_store_role_menu", "yoshop_store_user_role"]
  16. for t in TABLES:
  17. cur.execute("select * from " + t)
  18. cols = [d[0] for d in cur.description]
  19. rows = [dict(zip(cols, r)) for r in cur.fetchall()]
  20. json.dump(rows, open("backup/%s.json" % t, "w", encoding="utf-8"), ensure_ascii=False, default=str)
  21. print(t, len(rows))
  22. cur.execute("select store_user_id,user_name,real_name,is_super from yoshop_store_user")
  23. print("用户:", cur.fetchall())
  24. cur.execute("select role_id,role_name,parent_id from yoshop_store_role")
  25. print("角色:", cur.fetchall())
  26. cur.execute("select address_id,type,name from yoshop_store_address")
  27. print("地址:", cur.fetchall())
  28. cur.execute("select page_id,page_type,page_name,is_delete from yoshop_page")
  29. print("页面:", cur.fetchall())
  30. conn.close()