我有以下问题。我在服务器上有一个生产表,它类似于12GB,我想要的只是从生产中提取数据,并将其导入到我的本地数据库中,以便于操作。
最后,我想以CSV或Excel格式将数据提交给客户端。但是,我需要有自动化的脚本,可以导入每个表的数据到一个csv文件,如命名它的表名或什么?你真的能帮上忙吗,我很感激?
发布于 2014-04-30 15:07:38
查看带有--tab选项的mysqldump。它每个故事输出一个文件,根据表名命名文件,并且文件的内容是制表符分隔的文本。
请参阅http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html#option_mysqldump_tab
然后,您可以将文本文件导入到Excel中,并且可以使用change the delimiter between fields to tab。
您可能还希望将数据直接导入Excel,而不需要使用中间文本文件。请参阅http://dev.mysql.com/doc/mysql-for-excel/en/mysql-for-excel-import.html
发布于 2018-09-08 12:03:33
在一些研究之后,我所做的最简单的方法是运行python脚本。将所有需要的表放在一个列表中,并循环遍历该列表。以下python脚本会将所有表导出到相应的csv文件中
import MySQLdb
import csv
user = '' # your username
passwd = '' # your password
host = '' # your host or localhost if running locally
db = '' # database where your table is stored
dest_folder = '' # destination folder for the files to be written
table_list = []
for table in table_list:
print(table)
con = MySQLdb.connect(user=user, passwd=passwd, host=host, db=db)
cursor = con.cursor()
query = "SELECT * FROM %s;" % table
cursor.execute(query)
with open(folder + table + '.csv','w') as f:
writer = csv.writer(f)
for row in cursor.fetchall():
writer.writerow(row)如果出现no module named MySQLdb错误,请使用以下方法安装MySQLdb
1. easy_install mysql-python
2. pip install mysql-python
3. pip install mysqlclient
4. apt-get install python-mysqldb (Ubuntu - my preferred OS)https://stackoverflow.com/questions/23381677
复制相似问题