-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-cities_by_state.py
executable file
·48 lines (39 loc) · 1.07 KB
/
4-cities_by_state.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
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python3
"""Retrieving safely all connected states and cities from the db
using MySQLdb connector
"""
def main(MYSQL_USERNAME, MYSQL_PASSWD, DB_NAME):
"""Retrieving safely all connected states and cities from the db
Arguments:
MYSQL_USERNAME -- DB username
MYSQL_PASSWD -- DB password
DB_NAME -- DB name
"""
conn = MySQLdb.Connect(
host="localhost",
port=3306,
user=MYSQL_USERNAME,
passwd=MYSQL_PASSWD,
db=DB_NAME,
charset="utf8")
cur = conn.cursor()
cur.execute(
"""SELECT cities.id,
cities.name,
states.name
FROM cities JOIN states
ON states.id = cities.state_id
ORDER BY cities.id ASC
""")
data = cur.fetchall()
for row in data:
print(row)
cur.close()
conn.close()
if __name__ == '__main__':
import MySQLdb
import sys
MYSQL_USERNAME = sys.argv[1]
MYSQL_PASSWD = sys.argv[2]
DB_NAME = sys.argv[3]
main(MYSQL_USERNAME, MYSQL_PASSWD, DB_NAME)