![](/img/trans.png)
[英]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.