使用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
相关产品推荐
相关产品推荐

