2017-10-13 69 views
1

我更新谷歌的輸出反地理編碼(這是JSON格式)更新爲nvarchar(max)列,錯誤從Python的

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=mydb;UID=test;[email protected];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的那些列執行任務。

有沒有人有任何想法呢?

+2

你確定'pw'的類型是'str'嗎?可能試試'cursor.execute(「UPDATE GEOCODE_tbl SET JSON_str =?WHERE GEOCODE_ID =?」,(str(pw),749904))'? – FlipperPA

+0

@FlipperPA是將它們轉換爲str工作。非常感謝!!! – deepesh

+1

太好了,我會將其轉換爲答案! – FlipperPA

回答

1

pw不是str類型。嘗試將您的查詢轉換爲:

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

祝你好運!