2016-05-16 99 views
1

我想從Track Table更新TrackId的UnitPrice。 UnitPrice和TrackId通過用戶輸入給出參數。我目前正在接受對參數的數目錯誤:多佔位符SQLite蟒蛇不工作

Traceback (most recent call last): 
    File "HW4.py", line 48, in <module> 
    conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", (n_price,t_id)) 
    sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type. 

這裏是我的代碼:

track_id = raw_input('Enter Track ID\n') 
t_id = (track_id,) 
cur = conn.execute("SELECT T.UnitPrice FROM Track T WHERE T.TrackId = ?", t_id) 
for row in cur: 
    print "Unit Price: ", row[0] 

new_price = raw_input('Enter New Price\n') 
n_price = (new_price,) 
conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", n_price, t_id) 
conn.commit 
print "Total number of rows updated: ", conn.total_changes 

cur = conn.execute("SELECT T.UnitPrice FROM Track T WHERE T.TrackId = ?", t_id) 
for row in cur: 
    print "Unice Price: ", row[0] 

我猜這是我如何把n_price和T_ID到語法錯誤?佔位符。

+0

您追蹤消息不匹配您的發佈代碼。 –

回答

1

你需要在你的參數傳遞的單一迭代,就像一個列表或元組,但你包裹在另一個你n_price參數,嵌套的元組:所以現在

n_price = (new_price,) # this is a tuple 
conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", 
      (n_price, t_id)) # wrapping in another tuple 

你正在傳入((new_price,), t_id),但該第一個元組不是受支持的類型。

傳遞到自己的論點集中在一個元組:

params = (new_price, t_id) 
conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", params) 

或使用

conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", (new_price, t_id)) 
+0

快速回復。修復問題。很好的解釋。高超。 –