dump_tables.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. # -*- coding: utf-8 -*-
  2. """导出指定表结构,用于写 Java 实体"""
  3. import re
  4. import sys
  5. import pymysql
  6. from _root import ROOT
  7. YML = str(ROOT / "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. TABLES = sys.argv[1:]
  11. if not TABLES:
  12. print("用法: dump_tables.py yoshop_store yoshop_category ...")
  13. raise SystemExit(1)
  14. conn = pymysql.connect(host="192.168.1.177", port=3306, user="root", password=pwd, database="shop")
  15. cur = conn.cursor()
  16. placeholders = ",".join(["%s"] * len(TABLES))
  17. cur.execute(
  18. "SELECT table_name,column_name,column_type,column_default,column_comment "
  19. "FROM information_schema.columns WHERE table_schema='shop' AND table_name IN (" + placeholders + ") "
  20. "ORDER BY table_name, ordinal_position", TABLES)
  21. cur_t = None
  22. for tn, cn, ct, df, cm in cur.fetchall():
  23. if tn != cur_t:
  24. print("--", tn)
  25. cur_t = tn
  26. print(f" {cn} {ct} default={df} {cm}")
  27. conn.close()