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()
相关推荐
爬山算法6 小时前
Redis(162)如何使用Redis实现消息队列?
数据库·redis·缓存
郑州光合科技余经理6 小时前
同城系统海外版:一站式多语种O2O系统源码
java·开发语言·git·mysql·uni-app·go·phpstorm
u***32436 小时前
【Redis】centos7 systemctl 启动 Redis 失败
数据库·redis·缓存
煎蛋学姐6 小时前
SSM社区生鲜电商平台dq96z(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·用户管理·ssm 框架·社区生鲜电商·商品信息管理
杨云龙UP6 小时前
从0搭建Oracle ODA NFS异地备份:从YUM源到RMAN定时任务的全流程
linux·运维·数据库·oracle
倔强的石头_7 小时前
从 Oracle 到 KingbaseES:破解迁移痛点,解锁信创时代数据库新可能
数据库
踢足球09297 小时前
Redis的典型应用
数据库·redis·缓存
hadage2337 小时前
--- redis 常见问题 ---
数据库·redis·mybatis
O***P5717 小时前
redis批量删除namespace下的数据
数据库·redis·缓存
5***26228 小时前
SQL Server导出和导入可选的数据库表和数据,以sql脚本形式
数据库·sql