无法使用 pyodbc 在 Access 数据库中的 table 中 insert/update 长文本字段

Cannot insert/update Long Text field in a table in an Access database using pyodbc

我一直在使用 pyodbc 连接本地 Access 数据库(.accdb 文件),但无法更新长文本字段,如果我尝试输入的字符串超过 255 个字符。该字段(Description)的数据类型为Long Text,Text Format为Rich Text。

代码:

import pyodbc
pyodbc.pooling = False

conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=C:\...\...\...\...\database.accdb;'
    )
cnxn = pyodbc.connect(conn_str)
crsr = cnxn.cursor()

temp_desc = complex_descricao(comp_idx, "T_Compound", crsr, cnxn) #Function produces large string (> 255 characters)

#temp_desc = temp_desc[:255]   # Filter I have used to test the maximum number of characters allowed

crsr.execute("UPDATE T_Compound SET Description = ? WHERE ID_Comp = ?", temp_desc, comp_idx)
cnxn.commit()

这会产生以下错误:

Error: ('HY104', '[HY104] [Microsoft][ODBC Microsoft Access Driver]Invalid precision value (98) (SQLBindParameter)')

我试过直接在数据库中更改字段属性。我也试过在acces中直接插入超过255个字符的字符串,成功了。

我已经搜索了解决问题的方法,我认为它与绑定参数有关,尽管我对这个主题了解不多。我在这里留下一个 link 我相信它可能与问题有关:https://github.com/mkleehammer/pyodbc/wiki/Binding-Parameters

如果您能帮助我们使用大字符串更新字段,我们将不胜感激。

您在 pyodbc 4.0.22 中遇到了回归问题。它正在调查中(多个 GitHub 问题 here)并且应该在 pyodbc 的未来版本中修复。同时,只需恢复到 pyodbc 4.0.21:

pip install pyodbc==4.0.21