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()
相关推荐
韩立学长3 分钟前
基于Springboot酒店管理系统的设计与实现c12044zy(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
han_hanker4 分钟前
统一拦截异常 @RestControllerAdvice
java·开发语言·数据库
ohoy20 分钟前
EasyPoi 数据脱敏
开发语言·python·excel
herinspace25 分钟前
管家婆软件年结存后快马商城操作注意事项
服务器·数据库·windows
qq_348231851 小时前
MySQL 与 PostgreSQL PL/pgSQL 的对比详解
数据库·mysql·postgresql
玩转数据库管理工具FOR DBLENS1 小时前
DBLens:开启数据库管理新纪元——永久免费,智能高效的国产化开发利器
数据结构·数据库·测试工具·数据库开发
芝麻馅汤圆儿1 小时前
sockperf 工具
linux·服务器·数据库
IndulgeCui1 小时前
金仓数据库征文_使用KDTS迁移mysql至金仓数据库问题处理记录分享
数据库
cui_win1 小时前
Prometheus实战教程 - mysql监控
mysql·prometheus·压测
wsx_iot1 小时前
mysql的快照读和当前读
数据库·mysql