0f35b7e1
tangwang
tests
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#!/usr/bin/env python3
"""查询 buy_buyer 表的字段信息"""
import os
import pymysql
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from config.offline_config import DB_CONFIG
def query_table_fields():
"""查询表结构"""
try:
# 连接数据库
connection = pymysql.connect(
host=DB_CONFIG['host'],
port=int(DB_CONFIG['port']),
user=DB_CONFIG['username'],
password=DB_CONFIG['password'],
database=DB_CONFIG['database'],
charset='utf8mb4'
)
with connection.cursor() as cursor:
# 查询表结构
sql = "DESCRIBE buy_buyer"
cursor.execute(sql)
result = cursor.fetchall()
print("=" * 100)
print(f"{'字段名':<30} {'类型':<20} {'允许NULL':<10} {'键':<10} {'默认值':<15} {'额外信息'}")
print("=" * 100)
for row in result:
field = row[0]
type_info = row[1]
null = row[2]
key = row[3]
default = row[4]
extra = row[5]
print(f"{field:<30} {type_info:<20} {null:<10} {key:<10} {str(default):<15} {extra}")
print("=" * 100)
print(f"\n总计: {len(result)} 个字段")
except Exception as e:
print(f"查询失败: {e}")
finally:
if 'connection' in locals():
connection.close()
if __name__ == "__main__":
query_table_fields()
|