You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pandas to_sql写入SQL Server VARBINARY(MAX)时隐式转换报错

问题:QThread下pandas to_sql写入SQL Server varbinary(MAX)字段报错

我编写了一个appendTable()方法,接收表名和columns=data形式的关键字参数,将其构建为DataFrame后调用dataframe.to_sql()向Microsoft SQL Server的表中追加数据,代码如下:

def appendTable(self, tableName, **kwargs):
        dataFrame = pd.DataFrame(data=[kwargs])
        print(dataFrame)

        with self.connection_handling():
            with threadLock:
                dataFrame.to_sql(tableName, con=self.connection.dbEngine, schema="dbo", index=False, if_exists='append')

调用示例:

self.appendTable(tableName="Notebook", FormID=ID, CompressedNotes=notebook)

Notebook表结构如下:

NotebookID       | int            | primary auto-incrementing key
FormID           | int            | foreign key to a form table
Notes            | varchar(MAX)   | allow-nulls : True
CompressedNotes  | varbinary(MAX) | allow-nulls : True

其中CompressedNotes的数据来自PyQt5 TextEdit的HTML内容,经编码和zlib.compress()压缩后得到bytes类型:

notebook_html = self.noteBookTextEdit.toHtml()
notebookData  = zlib.compress(notebook_html.encode())

打印验证数据类型为<class 'bytes'>,生成的SQL语句为:

SQL: INSERT INTO dbo.[Notebook] ([FormID], [CompressedNotes]) VALUES (?, ?)
parameters: ('163', b'x\x9c\x03\x00\x00\x00\x00\x01')

近期将该方法从threading.Thread改为QThread执行后,出现报错:

Could not execute cursor!
Reason: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Implicit conversion from data type varchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query. (257) (SQLExecDirectW); 
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)')
[SQL: INSERT INTO dbo.[Notebook] ([FormID], [CompressedNotes]) VALUES (?, ?)]
[parameters: ('163', b'x\x9c\x03\x00\x00\x00\x00\x01')]

但直接使用pyodbc cursor执行相同参数的SQL语句却能正常运行:

cursor.execute('INSERT INTO Notebook (FormID, CompressedNotes) VALUES (?, ?)', (FormID, notebook))

我使用的是Python 3.11.2和pandas 1.5.3,之前用threading.Thread时无此问题,请问是否是pandas.DataFrame()转换了bytes类型,或是存在其他未注意到的问题?

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:40:01