2013-12-12 37 views
-1

我不斷收到錯誤在Python錯誤「錯誤號碼提供給綁定」:什麼呢使用SQLite

Incorrect number of bindings supplied 

CCAN誰能告訴我這是什麼意思?

在那裏我得到的錯誤代碼是:

import sqlite3 
statement='' 
name='' 
def update_data(values): 
    with sqlite3.connect("mrsimms.db")as db: 
     cursor = db.cursor() 
     sql = statement 
     cursor.execute(sql,values) 
     db.commit() 

def update_product_type(): 
    statement= "Update Stock set Type=? where Name=?" 
    Type=input("Please enter the updated type: ") 
    data=(Type) 
    update_data(data) 
    print() 
    again=input('Would you like to do anything else?(y/n) ') 
    if again =='y': 
     menu() 
    else: 
     print() 
def update_product_retail_price(): 
    statement= "Update Stock set RetailPrice=? where Name=?" 
    RetailPrice=input("Please enter the updated retail price: ") 
    data=(RetailPrice) 
    update_data(data) 
    print() 
    again=input('Would you like to do anything else?(y/n) ') 
    if again =='y': 
     menu() 
    else: 
     print() 

def update_product_number_in_stock(): 
    statement= "Update Stock set NumberInStock=? where Name=?" 
    NumberInStock=input("Please enter the updated number in stock: ") 
    data=(NumberInStock) 
    update_data(data) 
    print() 
    again=input('Would you like to do anything else?(y/n) ') 
    if again =='y': 
     menu() 
    else: 
     print() 


def menu(): 
    Name = input("Please enter the name of the product you wish to update: ") 
    print() 
    print('1. Update the product type') 
    print('2. Update the product retail price') 
    print('3. Update number in stock ') 
    print('9. Quit ') 
    print() 
    choice=int(input('What would you like to do? ')) 
    if choice == 1: 
     update_product_type() 
    elif choice == 2: 
     update_product_retail_price() 
    elif choice == 3: 
     update_product_number_in_stock() 
    elif choice == 9: 
     print() 

menu() 

我試圖編輯我的數據庫中的記錄,我不能看到我做了什麼錯。任何幫助將是可愛的,我是新的Python使用數據庫。

+0

該代碼甚至不起作用,因爲您沒有將'statement'傳遞給'update_data'。 –

回答

1

您需要確保SQL查詢中變量的數量與您提供的值的數量相同。您似乎只用一個值調用update_data,而不是必需的值。

當行cursor.execute(sql,values)被執行時,它需要同時具有查詢和正確數量的值。例如,查詢"Update Stock set NumberInStock=? where Name=?"需要有兩個值:每個問號一個。

否則,您會收到一個錯誤,提示您沒有提供正確的綁定數量。

4

您正在傳遞一個單個字符串作爲sql.execute()調用的第二個參數;字符串也是序列,其長度等於字符串中的字符數。

你要使它成爲一個元組,而是和你想,卻忘了一個逗號:

data=(NumberInStock) 

應該

data=(NumberInStock,) 

(在幾個地方和類似的錯誤)。

接下來,您的查詢實際上需要兩個參數;您也必須傳遞Name部分查詢的值。

見你的第一個查詢,例如:

statement= "Update Stock set Type=? where Name=?" 

在這裏,你拿一個類型和名稱,但只有在該類型的值傳遞:

Type=input("Please enter the updated type: ") 
data=(Type) 
update_data(data) 

您需要添加一個值名稱參數太:

Type = input("Please enter the updated type: ") 
data = (Type, Name) 
update_data(data) 

其中Name應作爲參數添加到喲你的職能,你應該通過statement作爲參數到你的update_data()函數;這裏不使用全局變量:

import sqlite3 

def update_data(sql, values): 
    with sqlite3.connect("mrsimms.db") as db: 
     cursor = db.cursor() 
     cursor.execute(sql, values) 
     db.commit() 

def update_product_type(Name): 
    statement = "Update Stock set Type=? where Name=?" 
    Type = input("Please enter the updated type: ") 
    data = (Type, Name) 
    update_data(data) 

def update_product_retail_price(Name): 
    statement = "Update Stock set RetailPrice=? where Name=?" 
    RetailPrice = input("Please enter the updated retail price: ") 
    data = (RetailPrice, Name) 
    update_data(statement, data) 

def update_product_number_in_stock(): 
    statement = "Update Stock set NumberInStock=? where Name=?" 
    NumberInStock = input("Please enter the updated number in stock: ") 
    data = (NumberInStock, Name) 
    update_data(data) 


def menu(): 
    while True: 
     Name = input("Please enter the name of the product you wish to update: ") 
     print() 
     print('1. Update the product type') 
     print('2. Update the product retail price') 
     print('3. Update number in stock ') 
     print('9. Quit ') 
     print() 
     choice = int(input('What would you like to do? ')) 

     if choice == 1: 
      update_product_type(Name) 
     elif choice == 2: 
      update_product_retail_price(Name) 
     elif choice == 3: 
      update_product_number_in_stock(Name) 
     elif choice == 9: 
      print() 
      break 

     again = input('Would you like to do anything else? (y/n) ') 
     if again.lower() != 'y': 
      break 

我搬到了問題做別的了菜單,使用循環而不是遞歸。