[英]Error Inserting data into MS SQL Server Using Python pyodbc[Error 42000]
[英]Pyodbc: Errors Inserting pickled Python Model to MS SQL Server
一直在尝试解决这个问题,但遇到了很多问题。 想知道是否有人以前见过这个。
我正在尝试在 Python 中pickle
一个简单的 RandomForestClassifier (sklearn),并使用pyodbc
将其保存到 MS SQL Server 数据库中。 特别是,我正在使用UPDATE
语句,因为我正在更新以前训练过的模型。
这是我正在使用的查询:
RF_serialized = pickle.dumps(RF)
RF_serialized_ins = str(RF_serialized)[1 : ] # doing this to cut off the leading 'b' from
# Python's byte data, per suggestions from other answers
q = "UPDATE table \
SET serializedModel = CONVERT(VARBINARY(MAX), {}) \
WHERE IDa = {} AND \
IDb = {} AND \
IDc = {}".format(RF_serialized_ins, "x", "y", "z")
但是,我不断收到以下非特定类型的错误:
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server]Syntax error, permission violation, or other nonspecific error (0) (SQLExecDirectW)')
有人遇到过这个吗? 我肯定 ID 和过滤器是正确的,等等。目标列的数据类型是VARBINARY(MAX)
。 一个想法:腌制的物体太大了吗? 物体尺寸:
print("Type of python object:", type(RF_serialized))
print("The size of the pickled RF model is:", RF_serialized.__sizeof__())
Type of python object: <class 'bytes'>
The size of the pickled RF model is: 5487942
这是最终工作的结果(感谢@Gord Thompson 让我朝着正确的方向前进):
pyodbc
标准 - 而不是 Python 的.format()
。 我们最终将查询更改为以下内容:q = "UPDATE table \
SET serializedModel = CONVERT(VARBINARY(MAX), ?) \
WHERE IDa = CONVERT(uniqueidentifier, ?) AND \
IDb = CONVERT(uniqueidentifier, ?) AND \
IDc = CONVERT(uniqueidentifier, ?)"
args = (RF_serialized,
"x",
"y",
"z")
cursor.execute(q, args)
cnxn.commit()
CONVERT(uniqueidentifier, ?)
而不是尝试为字符串放入特殊字符(例如\\'
),因为 SQL Server 将 GUID/唯一标识符视为数据类型。.execute()
与我实际上试图修复的查询完全混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.