Created
January 12, 2014 15:12
-
-
Save mportocarrero/8385830 to your computer and use it in GitHub Desktop.
Query 22 - What was the evolution of people working and cycling everyday to work between 2001 and 2011 in Tower Hamlets?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Query in MySql: | |
| mysql> select work_2001, bike_2001, work_2011, bike_2011 from cycling where borough = 'Tower Hamlets'; | |
| +-----------+-----------+-----------+-----------+ | |
| | work_2001 | bike_2001 | work_2011 | bike_2011 | | |
| +-----------+-----------+-----------+-----------+ | |
| | 73918 | 2214 | 120873 | 8112 | | |
| +-----------+-----------+-----------+-----------+ | |
| 1 row in set (0.00 sec) | |
| Outuput results in Python: | |
| import pymysql, sys | |
| import csv | |
| try: | |
| # Connect to the database | |
| conn = pymysql.connect(host='localhost', | |
| port=3306, | |
| user='co302mc', | |
| passwd='mafol597', | |
| db='co302mc_bike_theft') | |
| # Create a cursos to query the DB | |
| cur = conn.cursor() | |
| cur.execute("SELECT work_2001, bike_2001, work_2011, bike_2011 from cycling where borough = 'Tower Hamlets'") | |
| data = cur.fetchall() | |
| c = csv.writer(open("th_bike_evolution.csv","wb")) | |
| for row in data: | |
| print row | |
| c.writerow(row) | |
| except Exception as e: | |
| print "couldn't connect to database",e | |
| sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment