Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mportocarrero/8385383 to your computer and use it in GitHub Desktop.
Save mportocarrero/8385383 to your computer and use it in GitHub Desktop.
Query 16 - What is evolution of the percentage of people cycling everyday to work between 2001 and 2011 in Lewisham? This query shows if the percentage of people cycling everyday to work (from the people that work) increased or decreased between 2001 and 2011. It can be complemented with the difference between the two values.
Query in MySql:
mysql> select percentage_2001, percentage_2011 from cycling where borough = 'Lewisham' group by borough;
+-----------------+-----------------+
| percentage_2001 | percentage_2011 |
+-----------------+-----------------+
| 1.8 | 4.0 |
+-----------------+-----------------+
1 row in set (0.01 sec)
Output 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 percentage_2001, percentage_2011 from cycling where borough = 'Lewisham' group by borough")
data = cur.fetchall()
c = csv.writer(open("lew_percentage_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