dump_tables.py 1.0 KB

1234567891011121314151617181920212223242526272829
  1. # -*- coding: utf-8 -*-
  2. """导出指定表结构,用于写 Java 实体"""
  3. import re
  4. import sys
  5. import pymysql
  6. YML = r"C:\Users\Administrator\WorkBuddy\2026-10-17-12-27-10\java-rewrite\yoshop-java\src\main\resources\application.yml"
  7. text = open(YML, encoding="utf-8").read()
  8. pwd = re.findall(r"password:\s*(.+)", text)[0].strip().strip("\"'")
  9. TABLES = sys.argv[1:]
  10. if not TABLES:
  11. print("用法: dump_tables.py yoshop_store yoshop_category ...")
  12. raise SystemExit(1)
  13. conn = pymysql.connect(host="192.168.1.177", port=3306, user="root", password=pwd, database="shop")
  14. cur = conn.cursor()
  15. placeholders = ",".join(["%s"] * len(TABLES))
  16. cur.execute(
  17. "SELECT table_name,column_name,column_type,column_default,column_comment "
  18. "FROM information_schema.columns WHERE table_schema='shop' AND table_name IN (" + placeholders + ") "
  19. "ORDER BY table_name, ordinal_position", TABLES)
  20. cur_t = None
  21. for tn, cn, ct, df, cm in cur.fetchall():
  22. if tn != cur_t:
  23. print("--", tn)
  24. cur_t = tn
  25. print(f" {cn} {ct} default={df} {cm}")
  26. conn.close()