-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassengers0.py
More file actions
36 lines (28 loc) · 1.15 KB
/
passengers0.py
File metadata and controls
36 lines (28 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def main():
# List all flights.
flights = db.execute("SELECT id, origin, destination, duration FROM flights").fetchall()
for flight in flights:
print(f"Flight {flight.id}: {flight.origin} to {flight.destination}, {flight.duration} minutes.")
# Prompt user to choose a flight.
flight_id = int(input("\nFlight ID: "))
flight = db.execute("SELECT origin, destination, duration FROM flights WHERE id = :id",
{"id": flight_id}).fetchone()
# Make sure flight is valid.
if flight is None:
print("Error: No such flight.")
return
# List passengers.
passengers = db.execute("SELECT name FROM passengers WHERE flight_id = :flight_id",
{"flight_id": flight_id}).fetchall()
print("\nPassengers:")
for passenger in passengers:
print(passenger.name)
if len(passengers) == 0:
print("No passengers.")
if __name__ == "__main__":
main()