2017-04-06 76 views
-1

爲什麼他給我(類型錯誤)在這個陳述 「address = cur.fetchone()[2] last = cur.fetchone()[4] no = cur.fetchone()[5],而它接受」名稱= cur.fetchone()[1]「中的代碼:」爲什麼它給我在該代碼中輸入錯誤?

import sqlite3 
conn = sqlite3.connect('myproject.sqlite') 
cur = conn.cursor() 
print "Welcome Mr/Hefnawy" 
cur.execute('SELECT phone FROM participants') 
b = cur.fetchone()[0] 
while True: 
    a = raw_input("Enter Phone number here : ") 
    if a == b : 
     cur.execute('SELECT name,address,last_order,no_of_orders FROM participants WHERE phone = ?',(b,)) 
     name = cur.fetchone()[1] 
     address = cur.fetchone()[2] 
     last = cur.fetchone()[4] 
     no = cur.fetchone()[5] 
     print "The Costumer is already exist in Paricipants " 
     print "To edit the costumer data press (1)", "\n" , "to delet the costumer press (2)", "\n" , "add new order to the costumer press (3) " 
     c = raw_input("Enter your choice here : ") 
     if c == "1": 
      print "What do you want to edit ? , to edit name press 1 , to edit address press 2 , to edit phone press 3" 
      d = raw_input("Enter your choice here : ") 
      if d == "1" : 
       e = raw_input("New costumer name please ") 
       cur.execute('UPDATE participants SET name = ? WHERE phone = ?' , (e , a)) 
       print "Costumer name has been updated to :", e 
       print "" 
       conn.commit() 
    else: 
     print "The costumer is not exist" 
     print b 
     print a , type(a) 
+0

請填寫錯誤信息 –

回答

0

當你從遊標中讀取的東西例如說

t = cur.fetchone() 

可以從噸使用 訪問數據print t[0],t[1],t[2]但在你的情況下,您正在使用多個cur.fetchone()它允許您使用name = cur.fetchone()[1]結束數據在光標。第二行address = cur.fetchone()[2]及其後面的行沒有執行sql查詢以供它們讀取,因此會給出錯誤。如果要訪問整行,只需將其分配給一個變量並使用該變量來獲取數據。

相關問題