-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-model_state_my_get.py
38 lines (30 loc) · 1 KB
/
10-model_state_my_get.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
37
38
#!/usr/bin/python3
"""
Prints the State object with the name passed as argument
from the database hbtn_0e_6_usa
"""
if __name__ == "__main__":
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from model_state import Base, State
from sys import argv
import re
if (len(argv) != 5):
print('Use: username, password, database_name, state')
exit(1)
searched = ' '.join(argv[4].split())
if (re.search('^[a-zA-Z ]+$', searched) is None):
print('Enter a valid name state (example: Arizona)')
exit(1)
engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(
argv[1], argv[2], argv[3]), pool_pre_ping=True)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
states = session.query(State).where(State.name == searched)
if (states.count() == 0):
print('Not found')
else:
for row in states:
print(row.id)
session.close()