gpt4 book ai didi

python (pyodbc):Run ms access query from python results to size error

转载 作者:太空宇宙 更新时间:2023-11-04 02:56:32 24 4
gpt4 key购买 nike

我在 python 3.5 上使用 pyodbc 和 pandas,以便将大约 10000 行数据从 MS Access 2010 加载到数据框中,然后计算一些相关性。

当我尝试使用下面的代码来查看生成器的功能时,代码打印了 ~10000 行中的大约 9600 行,然后出现以下错误:

[Microsoft][ODBC Microsoft Access Driver] The query cannot be completed. Either the size of the query is larger than the maximum size of a database (2 GB), or there is not enough temporary storage space on the disk to store the query result.

**Code:**

Import pyodbc
import pandas as pd

con = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=MyDB.mdb;')
cur - con.cursor()
sql = "SELECT * from [query table]" # I have a query table which is pivoting the data inside the access.

def gen(cursor, sql):
cursor.execute(sql)
While True:
row = cursor.fetchone()
if row is None:
break
yield row

for x in gen(cur,sql):
print(x)

我已经定义了另一个函数,它获取生成器的行并将它们附加到列表,然后附加到 pd.Dataframe,但生成器似乎没有完成这项工作。

我压缩并修复了数据库,但没有成功。另外,目前 mdb 文件的大小不超过 500mb。

你能告诉我如何克服这个错误吗?

非常感谢。

最佳答案

建立连接时,pyodbc 根据 Python 的 DB-API 规范默认为 autocommit=False。因此,当执行第一个 SQL 语句时,ODBC 开始一个数据库事务,该事务在 Python 代码执行 .commit().rollback() 之前一直有效连接。

显然,当您处理结果集中的行时,Access 数据库引擎积累了越来越多关于“事务”的信息,最终导致内存不足错误。将 pyodbc 连接设置为 autocommit=True 告诉 Access 数据库引擎不要费心跟踪任何与事务相关的信息,从而避免错误。

关于 python (pyodbc):Run ms access query from python results to size error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42176298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com