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()
相关推荐
IT教程资源C26 分钟前
(N-089)基于springboot网上订餐系统
mysql·springboot订餐系统
IT教程资源D42 分钟前
[N_083]基于springboot毕业设计管理系统
mysql·springboot毕业设计
韦东东44 分钟前
DeepSeek:R1本地RAG 问答: 功能新增,附 六大关键技术优化路径参考
数据库·mysql
Leon-Ning Liu1 小时前
19c RAC 环境 Patch 38326922 应用实战
数据库·oracle
虫小宝1 小时前
优惠券省钱app高并发秒杀系统:基于Redis与消息队列的架构设计
数据库·redis·缓存
赵渝强老师2 小时前
【赵渝强老师】MySQL的数据约束
数据库·mysql
半部论语2 小时前
MySQL 主机被封问题详解:原因、解除方法与预防策略
数据库·mysql
a587693 小时前
Oracle数据库体系结构深度解析:从内核到应用的理论全景
数据库·oracle
高溪流3 小时前
1.MySql概念讲解 及 MySql安装教程
数据库·mysql
语落心生3 小时前
深入doris查询计划以及io调度(四)存储引擎架构
数据库