TypeError:'float'对象没有属性'__getitem__'

时间:2013-02-07 18:22:21

标签: python sqlite

我使用以下代码在系统中进行预订但它返回错误:

conn = sqlite3.connect('SADS.db')
cur = conn.cursor()
choice = raw_input("What day would you like to book seats on? F/S : ")
if choice == "F":
    bookseats = [0,1]
    print"What seats would you like to book?"
    bookseats[0] = raw_input("Choice : ")
    bookseats[1] = raw_input("What is their user ID? : ")
    cur.execute("SELECT * FROM seats WHERE Booked='N'")
    rows = cur.fetchone()
    for row in rows:
        if row == None:
            break
        elif row[1] == bookseats[0] and row[4] == "N" and row[3] == "F":
            print "Seat Number:" , row[1] ,"Is now booked!"
            cur.execute("UPDATE seats SET Booked =N AND CustID=?", (bookseats[0]))

错误:

TypeError: 'float' object has no attribute '__getitem__'

我真的很困惑错误是什么以及我如何解决它?

2 个答案:

答案 0 :(得分:2)

您正在遍历rows = cur.fetchone()的结果,该结果只检索一行。删除该行,直接在光标上循环:

cur.execute("SELECT * FROM seats WHERE Booked='N'")
for row in cur:

您也不需要测试row == None,也不需要测试Booked列是否等于N。简化循环:

cur.execute("SELECT * FROM seats WHERE Booked='N' AND CustID=?", (bookseats[0],))
row = cur.fetchone()
if row is not None and row[3] == "F":
    print "Seat Number:" , row[1] ,"Is now booked!"
    cur.execute("UPDATE seats SET Booked =N AND CustID=?", (bookseats[0]))

您可以向数据库询问与bookseats[0]列匹配的行,以及第3列设置为F的位置。您没有向我们展示您的架构,因此很难建议您如何更新查询。

您的UPDATE查询不正确,您的意思是:

cur.execute("UPDATE seats SET Booked='Y' WHERE CustID=?", (bookseats[0],))

代替。

您的SQL查询和更新看起来很可疑;在不知道完整架构的情况下,它看起来好像是在一起混合你的座位号和客户号码。

答案 1 :(得分:2)

fetchone不返回行列表,只返回一行,所以这是不正确的:

rows = cur.fetchone()
for row in rows:

请改为:

row = cur.fetchone()