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()
相关推荐
逻辑驱动的ken1 小时前
Java高频面试考点18
java·开发语言·数据库·算法·面试·职场和发展·哈希算法
qq_392690661 小时前
Redis怎样应对Redis集群整体宕机带来的雪崩
jvm·数据库·python
快乐非自愿2 小时前
Redis--SDS字符串与集合的底层实现原理
数据库·redis·缓存
这儿有一堆花2 小时前
住宅代理(Residential Proxy)技术指南
开发语言·数据库·php
Eiceblue2 小时前
使用 C# 将 Excel 转换为 Markdown 表格(含批量转换示例)
开发语言·c#·excel
Java面试题总结2 小时前
使用 Python 设置 Excel 数据验证
开发语言·python·excel
茉莉玫瑰花茶2 小时前
LangChain 核心组件 [ 2 ]
java·数据库·langchain
存在的五月雨3 小时前
Mysql 索引的一些
数据库·mysql
黄俊懿4 小时前
MySQL主从复制:从“异步“到“GTID“,数据同步的进化之路
数据库·sql·mysql·oracle·架构·dba·db
看海的四叔4 小时前
【SQL】SQL-管好你的字符串
大数据·数据库·hive·sql·数据分析·字符串