Skip to content

Commit 804f097

Browse files
committedMar 20, 2024
list all cities
1 parent d8c4a8b commit 804f097

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python3
2+
"""
3+
script that lists all City objects from the database hbtn_0e_101_usa
4+
"""
5+
6+
import sys
7+
from relationship_state import Base, State
8+
from relationship_city import City
9+
from sqlalchemy import create_engine
10+
from sqlalchemy.orm import sessionmaker
11+
12+
if __name__ == "__main__":
13+
engine = create_engine(
14+
"mysql+mysqldb://{}:{}@localhost:3306/{}".format(
15+
sys.argv[1],
16+
sys.argv[2],
17+
sys.argv[3]
18+
), pool_pre_ping=True
19+
)
20+
Base.metadata.create_all(engine)
21+
Session = sessionmaker(bind=engine)
22+
session = Session()
23+
24+
states = session.query(State).order_by(State.id)
25+
26+
for state in states:
27+
for city in state.cities:
28+
print("{}: {} -> {}".format(city.id, city.name, state.name))

0 commit comments

Comments
 (0)
Please sign in to comment.