我正在从SQL Server读取非常大的表(大约是我的RAM的3倍)并将它们作为.csv写入块中。但是在.csv文件中缺少列名。我做错了什么?
我的代码如下:
import pandas as pd
import pyodbc
cnxn = pyodbc.connect('''Driver={SQL Server}; Server=myServer; > Database=myDb''')
cursor = cnxn.cursor()
#Extracting the naems of all the tables in the database
cursor.execute("select * from information_schema.tables")
tables = cursor.fetchall()
cursor.close()
#Reading all the tables and saving them chunk by chunk as csv
counter = 1
for t in tables:
print("Currently working on table Number {0} - {1}".format(counter, t[2]))
cursor = cnxn.cursor()
cursor.execute("Select * from {0}".format(t[2]))
file_name = "{0}.csv".format(t[2])
f = open(file_name, 'w')
# 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)
counter += 1
f.close()
cursor.close()
cnxn.close()