# config.py
USER = 'xx'
PASSWORD = 'yy'
HOSTS = (
('testA', 'xx', 3306),
('testB', 'yy', 3306),
)
# main.py
import pymysql
from config import USER, PASSWORD, HOSTS
def conn():
# 获取HOSTS里面的主机名和ip
for name,ip,port in HOSTS:
# 连接MySQL数据库
test = pymysql.connect(ip, USER, PASSWORD)
cursor = test.cursor()
cursor.execute("SELECT table_schema, table_name, table_rows, data_length + index_length FROM information_schema.TABLES;")
data = cursor.fetchall()
# 把数据写入文件
with open("f:\\test.csv", mode = 'a') as t:
#filed = 'hostname' + ',\t' + 'table_schema' + ',\t' + 'table_name' + ',\t' + 'table_rows' + ',\t' + 'data_length' + 'index_length' + '\n'
#t.write(filed)
for table_schema,table_name,table_rows,length in data:
res = str(name) + ',\t' + str(table_schema) + ',\t' + str(table_name) + ',\t' + str(table_rows) + ',\t' + str(length) + '\n'
t.write(res)
t.close()
test.close()
conn();
pymsql输出数据库数据到csv文件
最新推荐文章于 2022-11-25 14:57:08 发布