_export_remote_dict.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import pymysql
  2. from pathlib import Path
  3. from openpyxl import Workbook
  4. out = Path(r"d:/Projects/Ai-DOP/SourceCode/ai-dop-platform/docs/db/aidopcore_远端落库表字段说明.xlsx")
  5. conn = pymysql.connect(host='106.14.73.46', port=3306, user='aidopremote', password='AidOp#Remote2026$Secure', charset='utf8mb4', autocommit=True)
  6. try:
  7. with conn.cursor() as cur:
  8. cur.execute("""
  9. SELECT table_name, table_comment
  10. FROM information_schema.tables
  11. WHERE table_schema='aidopcore' AND table_name LIKE 'ado\\_%'
  12. ORDER BY table_name
  13. """)
  14. tables = cur.fetchall()
  15. wb = Workbook()
  16. ws1 = wb.active
  17. ws1.title = '表清单'
  18. ws1.append(['序号','表名','表说明'])
  19. for i, (t, c) in enumerate(tables, 1):
  20. ws1.append([i, t, c])
  21. ws2 = wb.create_sheet('字段说明')
  22. ws2.append(['表名','字段序号','字段名','类型','可空','默认值','字段说明'])
  23. for t, _ in tables:
  24. cur.execute("""
  25. SELECT ordinal_position, column_name, column_type, is_nullable, column_default, column_comment
  26. FROM information_schema.columns
  27. WHERE table_schema='aidopcore' AND table_name=%s
  28. ORDER BY ordinal_position
  29. """, (t,))
  30. cols = cur.fetchall()
  31. for pos, name, ctype, nullable, dft, cm in cols:
  32. ws2.append([t, pos, name, ctype, '是' if nullable=='YES' else '否', '' if dft is None else str(dft), cm])
  33. wb.save(out)
  34. print(str(out))
  35. print(f'tables={len(tables)}')
  36. finally:
  37. conn.close()