将购票和取消订单改为存储过程和触发器

This commit is contained in:
2024-06-14 21:26:48 +08:00
parent e435ae4035
commit 97e7382a6d
6 changed files with 84 additions and 50 deletions

View File

@@ -37,7 +37,7 @@ def book():
if not passengers:
flash("请至少添加一位乘客", "error")
return redirect(url_for('index'))
return redirect(url_for('search'))
conn = pymysql.connect(**db)
cursor = conn.cursor()
@@ -62,25 +62,8 @@ def book():
cursor.execute(price_sql, (flight_id,))
price = cursor.fetchone()[0]
insert_passenger_sql = """
INSERT INTO Passengers (ID, Name, Phone_number)
VALUES (%s, %s, %s)
ON DUPLICATE KEY UPDATE Name=VALUES(Name), Phone_number=VALUES(Phone_number);
"""
cursor.execute(insert_passenger_sql, (passenger_id, name, phone_number))
update_seat_sql = f"""
UPDATE Flights
SET {seat_class.replace(' ', '_').lower()}_seats_remaining = {seat_class.replace(' ', '_').lower()}_seats_remaining - 1
WHERE ID = %s
"""
cursor.execute(update_seat_sql, (flight_id,))
insert_ticket_sql = """
INSERT INTO Tickets (Price, FlightID, Seat_class, PassengerID, OrderID)
VALUES (%s, %s, %s, %s, %s)
"""
cursor.execute(insert_ticket_sql, (price, flight_id, seat_class, passenger_id, order_id))
# 调用存储过程
cursor.callproc('AddPassengerAndTicket', (passenger_id, name, phone_number, seat_class, flight_id, price, order_id))
conn.commit()
return redirect(url_for('order', order_id=order_id))
@@ -89,8 +72,8 @@ def book():
conn.rollback()
print(e)
flash("订票失败", "error")
return redirect(url_for('index'))
return redirect(url_for('search'))
finally:
cursor.close()
conn.close()
conn.close()