擅长:python、mysql、java
<p>您可以成批读取SQL数据库并将其写入文件,而不是一次读取整个数据库。有关如何添加到现有CSV文件的信息,请记入<a href="https://stackoverflow.com/questions/17530542/how-to-add-pandas-data-to-an-existing-csv-file">How to add pandas data to an existing csv file?</a>。</p>
<pre><code>import sqlite3
import pandas as pd
# Open the file
f = open('output.csv', 'w')
# Create a connection and get a cursor
connection = sqlite3.connect('mydata.sql')
cursor = connection.cursor()
# Execute the query
cursor.execute('select * from mydata')
# Get data in batches
while True:
# Read the data
df = pd.DataFrame(cursor.fetchmany(1000))
# We are done if there are no data
if len(df) == 0:
break
# Let's write to the file
else:
df.to_csv(f, header=False)
# Clean up
f.close()
cursor.close()
connection.close()
</code></pre>