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()
相关推荐
APguantou33 分钟前
NCRE-三级数据库技术-第14章-数据仓库与数据挖掘
数据库·数据仓库·数据挖掘
刘~浪地球2 小时前
Redis 从入门到精通(十):管道技术
数据库·redis·缓存
fzb5QsS1p5 小时前
MySQL 事务的二阶段提交是什么?
数据库·mysql
清风徐来QCQ8 小时前
Lombok/SSM/devTools
数据库
LaughingZhu8 小时前
Product Hunt 每日热榜 | 2026-04-05
前端·数据库·人工智能·经验分享·神经网络
2601_949814698 小时前
使用mysql报Communications link failure异常解决
数据库·mysql
搜佛说8 小时前
02-第2章-核心概念与架构
数据库·物联网·微服务·架构·边缘计算·iot
#六脉神剑10 小时前
MySQL参数调优:十个关键参数助力数据库性能数倍提升
运维·mysql
C'ᴇsᴛ.小琳 ℡10 小时前
高性能NoSQL
数据库·nosql
i220818 Faiz Ul11 小时前
动漫商城|基于springboot + vue动漫商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·动漫商城系统