-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaders.py
36 lines (28 loc) · 1.04 KB
/
readers.py
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 Books.
Books = db.execute("SELECT id, title, author, year FROM Books").fetchall()
for Book in Books:
print(f"Book {Book.id}: {Book.title} written by {Book.author}, {Book.year}")
# Prompt user to choose a Book.
Book_id = int(input("\nBook ID: "))
Book = db.execute("SELECT title, author, year FROM Books WHERE id = :id",
{"id": Book_id}).fetchone()
# Make sure Book is valid.
if Book is None:
print("Error: No such Book.")
return
# List readers.
readers = db.execute("SELECT name FROM readers WHERE Book_id = :Book_id",
{"Book_id": Book_id}).fetchall()
print("\nreaders:")
for reader in readers:
print(reader.name)
if len(readers) == 0:
print("No readers.")
if __name__ == "__main__":
main()