首页 > 解决方案 > 在 MS Access 中使用 to_sql(... , method='multi') 时出错

问题描述

我正在使用以下代码将数据插入 Microsoft Access 数据库:

test_data.to_sql('employee_table', cnxn, index=False, if_exists='append', chunksize=10, method='multi')

这给出了错误:

AttributeError: 'CompileError' object has no attribute 'orig'

仅使用以下即无method选项时没有错误:

test_data.to_sql('employee_table', cnxn, index=False, if_exists='append', chunksize=10)

标签: pythonpandasms-accesssqlalchemysqlalchemy-access

解决方案


您引用的错误消息是由原始错误引起的后续异常(在堆栈跟踪的早期):

sqlalchemy.exc.CompileError:具有当前数据库版本设置的“访问”方言不支持就地多行插入。

希望创建多行 INSERT 语句的method="multi"选项,通常以“表值构造函数”的形式,例如,.to_sql()

INSERT INTO table1 (col1, col2) VALUES (1, 'foo'), (2, 'bar')

Access SQL 不支持这些。

如果一个普通的.to_sql()(没有method=)对于一个大的 DataFrame 来说太慢了,那么考虑在 wiki 中记录的替代方法:

https://github.com/gordthompson/sqlalchemy-access/wiki/%5Bpandas%5D-faster-alternative-to-.to_sql()-for-large-uploads


推荐阅读