Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mportocarrero/8385830 to your computer and use it in GitHub Desktop.

Select an option

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?
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