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 小时前
mysql笛卡尔积怎么形成的怎么避免笛卡尔积
数据库·mysql
m0_564264184 小时前
IDEA DEBUG调试时如何获取 MyBatis-Plus 动态拼接的 SQL?
java·数据库·spring boot·sql·mybatis·debug·mybatis-plus
隐语SecretFlow5 小时前
隐语SecreFlow SCQL 1.0.0b1 发布:更完善的 SQL 支持与更高效的隐私查询引擎
数据库·sql
ttghgfhhjxkl5 小时前
文档搜索引擎搜索模块的索引更新策略:实时增量与全量重建设计
数据库·搜索引擎
老华带你飞5 小时前
机器人信息|基于Springboot的机器人门户展示系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·机器人·论文·毕设·机器人门户展示系统
StarRocks_labs5 小时前
StarRocks 在 Cisco Webex 的探索与实践
数据库·starrocks·json·存算分离·olap 技术栈
notion20255 小时前
Adobe Lightroom Classic下载与安装教程(附安装包) 2025最新版详细图文安装教程
java·数据库·其他·adobe
unicrom_深圳市由你创科技5 小时前
用 CTE 重构嵌套子查询:让复杂报表 SQL 可读性提升 80%
mysql·重构
左师佑图6 小时前
综合案例:Python 数据处理——从Excel文件到数据分析
开发语言·python·数据分析·excel·pandas
楚枫默寒6 小时前
mongodb备份脚本(单机+副本集)
数据库