简体   繁体   English

Python 读取 mysql 到 csv

[英]Python read mysql to csv

I would like to read a mysql database in chunks and write its contents to a bunch of csv files.我想分块读取 mysql 数据库并将其内容写入一堆 csv 文件。

While this can be done easily with pandas using below:虽然这可以通过 pandas 使用以下方法轻松完成:

df_chunks = pd.read_sql_table(table_name, con, chunksize=CHUNK_SIZE)

for i, df in enumerate(chunks):
    df.to_csv("file_{}.csv".format(i)

Assuming I cannot use pandas, what other alternative can I use?假设我不能使用 pandas,我还能使用什么其他替代方法? I tried using我尝试使用

import sqlalchemy as sqldb
import csv

CHUNK_SIZE = 100000
table_name = "XXXXX"


host = "XXXXXX"
user = "XXXX"
password = "XXXXX"
database = "XXXXX"
port = "XXXX"

engine = sqldb.create_engine('mysql+pymysql://{}:{}@{}:{}/{}'.format(user,password,host,port,database))
con = engine.connect()
metadata = sqldb.MetaData()

table = sqldb.Table(table_name, metadata, autoload=True, autoload_with=engine)
query = table.select()
proxy = con.execution_options(stream_results=True).execute(query)

cols = [""] + [column.name for column in table.c]
file_num = 0
while True:
    batch = proxy.fetchmany(CHUNK_SIZE)

    if not batch:
        break

    csv_writer = csv.writer("file_{}.csv".format(file_num), delimiter=',')
    csv_writer.writerow(cols)
    #csv_writer.writerows(batch) # while this work, it does not have the index similar to df.to_csv()

    for i, row in enumerate(batch):
        csv_writer.writerow(i + row) # will error here

    file_num += 1
proxy.close()

While using.writerows(batch) works fine, it does not have the index like the result you get from df.to_csv() .虽然 using.writerows(batch) 工作正常,但它没有像您从df.to_csv()获得的结果那样的索引。 I would like to add the row number equivalent as well, but cant seem to add to the row which is a sqlalchemy.engine.result.RowProxy .我也想添加等效的行号,但似乎无法添加到sqlalchemy.engine.result.RowProxy的行中。 How can I do it?我该怎么做? Or what other faster alternative can I use?或者我可以使用其他更快的替代方案吗?

Look up SELECT... INTO OUTFILE...查找SELECT... INTO OUTFILE...

It will do the task in 1 SQL statement;它将执行 1 SQL 语句中的任务; 0 lines of Python (other than invoking that SQL). 0 行 Python(调用该 SQL 除外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM