-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path10-model_state_my_get.py
More file actions
executable file
·41 lines (34 loc) · 1.13 KB
/
Copy path10-model_state_my_get.py
File metadata and controls
executable file
·41 lines (34 loc) · 1.13 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
37
38
39
40
41
#!/usr/bin/python3
"""
Module to perfom simple queries on the model_state model
using an ORM - SQLAlchemy
"""
from model_state import Base, State
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import sys
def connect_and_query(user: str, passwd: str, dbase: str, search: str) -> None:
"""
Connect to the database and make queries using ORM
Args:
user (str): mysql user
passwd (str): mysql password for `user`
dbase (str): Database to use
search (str): State to search for
"""
try:
engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'
.format(user, passwd, dbase))
Session = sessionmaker(bind=engine)
state_session = Session()
states = state_session.query(State).order_by(State.id).all()
for state in states:
if state.name == search:
print(state.id)
break
else:
print("Not found")
except Exception as e:
return e
if __name__ == "__main__":
connect_and_query(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])