| 123456789101112131415161718192021222324252627282930 |
- # -*- coding: utf-8 -*-
- """导出指定表结构,用于写 Java 实体"""
- import re
- import sys
- import pymysql
- from _root import ROOT
- YML = str(ROOT / "yoshop-java/src/main/resources/application.yml")
- text = open(YML, encoding="utf-8").read()
- pwd = re.findall(r"password:\s*(.+)", text)[0].strip().strip("\"'")
- TABLES = sys.argv[1:]
- if not TABLES:
- print("用法: dump_tables.py yoshop_store yoshop_category ...")
- raise SystemExit(1)
- conn = pymysql.connect(host="192.168.1.177", port=3306, user="root", password=pwd, database="shop")
- cur = conn.cursor()
- placeholders = ",".join(["%s"] * len(TABLES))
- cur.execute(
- "SELECT table_name,column_name,column_type,column_default,column_comment "
- "FROM information_schema.columns WHERE table_schema='shop' AND table_name IN (" + placeholders + ") "
- "ORDER BY table_name, ordinal_position", TABLES)
- cur_t = None
- for tn, cn, ct, df, cm in cur.fetchall():
- if tn != cur_t:
- print("--", tn)
- cur_t = tn
- print(f" {cn} {ct} default={df} {cm}")
- conn.close()
|