Sql 我可以在Python3中逐行写入DB查询的结果并跳过数据帧吗?
tl;dr:我正在寻找一种内存高效的方法,将22毫米记录写入以管道分隔的csv 我正在使用pyodbc和pandas将22MM记录拉入数据库。我的数据帧遇到内存问题。它被这么多的唱片噎住了。我知道有文件流方法,但我只将其用于组合CSV,而不是编写数据库查询结果。我是否可以在不首先访问RAM的情况下将查询流式传输到磁盘Sql 我可以在Python3中逐行写入DB查询的结果并跳过数据帧吗?,sql,python-3.x,csv,Sql,Python 3.x,Csv,tl;dr:我正在寻找一种内存高效的方法,将22毫米记录写入以管道分隔的csv 我正在使用pyodbc和pandas将22MM记录拉入数据库。我的数据帧遇到内存问题。它被这么多的唱片噎住了。我知道有文件流方法,但我只将其用于组合CSV,而不是编写数据库查询结果。我是否可以在不首先访问RAM的情况下将查询流式传输到磁盘 import sys import pandas as pd import pyodbc as db import time import os print("Connectin
import sys
import pandas as pd
import pyodbc as db
import time
import os
print("Connecting to SQL Server database" + time.strftime(' %H:%M:%S'))
connection_string = 'DSN=foo;'
conn = db.connect(connection_string)
print("Querying Database" + time.strftime(' %H:%M:%S'))
csr = conn.cursor()
sql = 'SELECT * FROM table'
df = pd.read_sql(sql,conn)
conn.commit()
csr.close()
conn.close()
#Write File
print('Writing results.csv'+ time.strftime(' %H:%M:%S'))
df.to_csv(os.path.join(pathvar, '_results.csv'), sep="|",index=False)
您可以执行以下操作:
sql = 'SELECT * FROM table'
csr.execute(sql)
for row in csr:
// Write the row to a file
我需要几个小时才能测试这个。谢谢