使用Python删除存储在SQLite 3数据库中的特定出租车预订
我是Python新手,在我的第一次大学作业中,我必须制作一个出租车预订系统,其中一个功能需要是用户可以删除以前的预订 我已经设法让预订的选择和列表工作,但我不知道如何删除一个,这里是我到目前为止使用Python删除存储在SQLite 3数据库中的特定出租车预订,python,sqlite,Python,Sqlite,我是Python新手,在我的第一次大学作业中,我必须制作一个出租车预订系统,其中一个功能需要是用户可以删除以前的预订 我已经设法让预订的选择和列表工作,但我不知道如何删除一个,这里是我到目前为止 def cancel_booking(user_id): print("\n") print("Cancel a Booking") with sqlite3.connect('Database.db') as db: cu
def cancel_booking(user_id):
print("\n")
print("Cancel a Booking")
with sqlite3.connect('Database.db') as db:
cursor = db.cursor()
# bookings which belong to the current user
sql = ''' SELECT * FROM booking WHERE customer_id=?'''
cursor.execute(sql, (user_id,))
bookings = cursor.fetchall()
# Listing all the available bookings
for i, b in enumerate(bookings, 1):
print(str(i) + ") ""Number: """ + str(b[3]) + " Postcode: """ + str(b[4]),
""" -> Number: """ + str(b[5]) + " Postcode: """ + str(b[6]))
select_delete = input("Please select the number of the booking that you would like to delete:")
index = int(select_delete)
booking = bookings[index]
bookingid = (booking[0],)
delete_booking = """DELETE FROM booking WHERE customer_id=?"""
cursor.execute(delete_booking, user_id)
db.commit()
在执行SQL的代码中添加预订的部分,用户id周围有括号
cursor.execute(sql, (user_id,))
但对于删除预订代码,它没有
cursor.execute(delete_booking, user_id)
是的,您需要为参数提供一个元组。通过添加包含逗号的
(用户id,)
!创建一个值为1的元组