【问题标题】:Error using to_sql(… , method='multi') with MS Access在 MS Access 中使用 to_sql(... , method='multi') 时出错
【发布时间】:2021-11-25 19:29:02
【问题描述】:

我正在使用以下代码将数据插入 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)

【问题讨论】:

  • 那为什么不使用不出错的代码呢?它是否提交数据?这里有什么问题?为什么需要附加参数?

标签: python pandas ms-access sqlalchemy sqlalchemy-access


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多