mysql数据库中多张表导出成excel方式

需求:

用于将mysql数据库中的几百张表导出成excel方式

表中有些字段的值是含有双引号和逗号值,比如json值

表中有些字段是汉字内容

导出的excel要求有表的列名

shell对于含有逗号和双引号的值会错乱分割

数据库中某些字段值是化学符号

python 复制代码
import pymysql,os,csv,shutil
target_dir=r'C:\Users\XXX\Desktop\excel'
if  os.path.exists(target_dir):
	shutil.rmtree(target_dir)
	os.mkdir(target_dir)
else:
	os.mkdir(target_dir)

myconn=pymysql.connect(host='localhost',port=3306,user='root',password='123456',database='rhtx')
table_sql="select table_name from information_schema.tables where table_schema='rhtx'"
with myconn.cursor() as cursor:
	cursor.execute(table_sql)
	table_name=list(cursor.fetchall())

for name in table_name:
	file_name=name[0]+".csv"
	file_path = os.path.join(target_dir, file_name)
	sql='select * from '+'rhtx.'+name[0]
	with myconn.cursor() as cursor:
		cursor.execute(sql)
		rows=cursor.fetchall()
		with open(file_path, mode='w', newline='',encoding='gbk') as file:
			writer=csv.writer(file)
			writer.writerow([i[0] for i in cursor.description])
			writer.writerows(rows)
myconn.close()
相关推荐
笃行3505 分钟前
数据集成软件里最贵的一行字,叫“用同步工具做增量同步“
数据库
吴声子夜歌6 分钟前
Docker入门与实战——Etcd(高可用的键值数据库)
数据库·docker·etcd
flytalei37 分钟前
从 Docker Compose 到 K3s:逐浪博客 MySQL 数据迁移实战与一致性验证
mysql·docker·容器
qq_白羊座42 分钟前
Hologres / PostgreSQL jsonb 常用函数 & 操作符对照表
数据库·postgresql
千桐科技43 分钟前
qData 数据中台专业版功能解析:告别 Excel 与 SQL 拼凑,打通系统合并的可追踪数据链路
数据库·sql·excel·qdata
jiushidt1 小时前
在 DBeaver 中为 SQLite 启用 SpatiaLite
数据库·sqlite
码士集团小青2 小时前
Google 开源了啥,让 AI Agent 碰数据库不再是定时炸弹
数据库·oracle
SL_staff2 小时前
3分钟上线新规则:业务人员拖拽完成合同超期自动暂停服务
java·mysql·全栈
CS创新实验室2 小时前
数据库突然断电,凭什么没丢数据?——WAL 日志与崩溃恢复
数据库·mysql
Warolitbos2 小时前
【数据库】SQL面试基础15题,测试/开发必背速查
数据库