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()
相关推荐
Database_Cool_2 分钟前
数据仓库弹性扩缩容实践:阿里云 AnalyticDB MySQL 按需付费方案详解
数据仓库·mysql·阿里云
星川皆无恙2 分钟前
基于BERT+LSTM+CRF与知识图谱的医疗智能问答系统实战:Neo4j图数据库+实体识别+意图分析完整项目
数据库·人工智能·深度学习·bert·lstm·知识图谱·neo4j
流星白龙3 分钟前
【MySQL高阶】27.事务(2)-锁
android·mysql·adb
Upsy-Daisy5 分钟前
Hermes Agent 学习笔记 03:CLI 与 TUI 使用体验,让 Agent 真正进入终端工作流
服务器·前端·数据库
数据库小学妹6 分钟前
MySQL并行复制原理与调优实战:LOGICAL_CLOCK到WRITESET_SESSION全链路优化
数据库·经验分享·mysql·性能优化·dba
周杰伦fans8 分钟前
深入浅出AutoCAD .NET二次开发:HostApplicationServices完全解析
数据库·.net
AQin10129 分钟前
【对比向】细算“成本”——Hive vs. Doris
大数据·数据库·hive·doris·实时数仓
承渊政道9 分钟前
【MySQL数据库学习】MySQL基本查询(上)
linux·数据库·学习·mysql·bash·数据库开发·数据库系统
OliverH-yishuihan9 分钟前
Excel中把一列数据转换成逗号隔开的一行
excel
学计算机的计算基9 分钟前
MySQL 性能调优面试复习:Explain、索引、慢查询、缓存和架构优化
java·数据库·笔记·mysql