I use the following code to make a booking in a system yet it returns an error:
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]))
Error:
TypeError: 'float' object has no attribute '__getitem__'
I am really confused what the error is and how I fix it?
You are looping over the result of
rows = cur.fetchone(), which retrieves just one row. Remove that line and just loop directly over the cursor:You do not need to test for
row == Noneeither, nor do you need to test if theBookedcolumn is equal toN. Simplify the loop to:You can just ask the database for rows that match your
bookseats[0]column, and where the 3rd column is set toF. You didn’t show us your schema so it is hard to recommend how you can update your query for that.Your
UPDATEquery is incorrect, you probably meant:instead.
Your SQL query and update look suspect though; without knowing the full schema it does look as if you are mixing your seat number and customer number in places.