2017-04-06 63 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查询以供它们读取,因此会给出错误。如果要访问整行,只需将其分配给一个变量并使用该变量来获取数据。

相关问题