0

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

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__'

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

4

2 回答 2

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]列匹配的行,以及第三列设置为的位置F。您没有向我们展示您的架构,因此很难推荐如何更新您的查询。

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

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

反而。

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

于 2013-02-07T18:26:28.987 回答
2

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

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

改为:

row = cur.fetchone()
于 2013-02-07T18:27:15.540 回答