从 Python 更新 nvarchar(max) 列时出错

Error on updating nvarchar(max) column from Python

我正在更新 Google 反向地理编码(采用 JSON 格式)的输出,

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=mydb;UID=test;PWD=abc@123;autocommit=True')
cursor = cnxn.cursor()
wp = urllib.request.urlopen("http://maps.googleapis.com/maps/api/geocode/json?latlng=18.5504,73.9412&sensor=false")
pw = wp.read()
#print(pw)
cursor.execute("UPDATE GEOCODE_tbl SET JSON_str = ? WHERE GEOCODE_ID = ?", pw,749904)
print('Done')
cnxn.commit()

但是报错

('22018', '[22018] [Microsoft][ODBC SQL Server Driver][SQL Server]Operand type clash: image is incompatible with nvarchar(max) (206) (SQLExecDirectW)')

这是什么错误? JSON_str 列有这样的 JSON 输出,所以我正在为那些 JSON_str 列为 NULL 的列执行任务。

有人知道吗?

pw 不是 str 类型。尝试将您的查询转换为:

cursor.execute("UPDATE GEOCODE_tbl SET JSON_str = ? WHERE GEOCODE_ID = ?", (str(pw), 749904))

祝你好运!