2016-02-21 153 views
0

我試圖將數據從一個字段移動到另一個字段。 這是我的代碼,但它不與更新語句的工作:SQLITE3從一個字段到另一個字段在同一個表和同一行中的數據傳輸

def update_ondemanddrama(Name): 
    with sqlite3.connect("sky_ondemand.db") as db: 
     cursor = db.cursor() 
     sql = "update TVshowsDrama set SecLastEp=LastEp where Name=?" 
     cursor.execute(sql, Name) 
     db.commit() 

工作

def insert_ondemanddrama(values): 
    with sqlite3.connect("sky_ondemand.db") as db: 
     cursor = db.cursor() 
     sql = "update TVshowsDrama set Name=?, LastEp=? where Name=?" 
     cursor.execute(sql,values) 
     db.commit() 

def insert_ondemanddoc(values): 
    with sqlite3.connect("sky_ondemand.db") as db: 
     cursor = db.cursor() 
     sql = "update TVshowsDoc set Name=?, LastEp=? where Name=?" 
     cursor.execute(sql,values) 
     db.commit() 


Type = int(input("Doc (1) or Drama (2)"))   
Name = input("Enter name of Show") 
LastEp = input("Enter Last episode aired (ex. s1e4)") 


if Type == 1: 
    if __name__== "__main__": 
     show = (Name, LastEp, Name) 
     insert_ondemanddoc(show) 
elif Type == 2: 
    if __name__== "__main__": 
     show = (Name, LastEp, Name) 
     update_ondemanddrama(Name) 
     insert_ondemanddrama(show) 
elif Type >=3: 
    print ("Incorrect entry") 

的錯誤我得到蟒蛇運行是這樣的:

Traceback (most recent call last): File "C:\Users\ict\Downloads\skyondemandv1.py", line 65, in <module> 
update_ondemanddrama(Name) File "C:\Users\ict\Downloads\skyondemandv1.py", line 34, in 
update_ondemanddrama cursor.execute(sql, Name) sqlite3.ProgrammingError: Incorrect number of bindings supplied. 
The current statement uses 1, and there are 5 supplied. 

回答

0

光標.execute期望一個迭代。當你給它一個字符串時,執行將它看作是一個可迭代的5個項目(5個字符)。 將執行行更改爲

cursor.execute(sql, (Name,)) 
+0

謝謝。這工作!它爲什麼在一個額外的圓括號中與Name一起使用?我還是不太明白。 – user5958076

+0

使用圓括號參數是元組(Name),而不是字符串Name。迭代第一個時,你會得到一個單一的字符串。當迭代第二個時,你會得到5個字符。 – noamk

+0

現在我收到了一個不同的錯誤,我確信程序的基礎使用相同的邏輯。錯誤消息如下:回溯(最近最後調用): 文件 「」,第1行,在 測試() 文件 「G:/test.py」,線路33,在測試 update_temp3(值) NameError:全局名稱「價值」沒有定義 – user5958076

相關問題