Skip to content

Instantly share code, notes, and snippets.

@mportocarrero
mportocarrero / Creating the database - MySql
Last active January 2, 2016 12:09
Creating the database - MySql I created the database with MySql and called it bike_theft.
mysql> create database co302mc_bike_theft;
Query OK, 1 row affected (0.00 sec)
mysql> exit;
@mportocarrero
mportocarrero / Creating table - Python (pymsql)
Created January 8, 2014 13:12
Creating table - Python (pymsql) I created a table called "bike" with the columns: "Borough", "Date", "Latitude", "Longitude", "Area" and "Investigation". To set up the data type for each column, I had a look online and found out which data type would best suit my purposes. Ended up finding about DECIMAL and the meaning of the values inside brac…
import pymysql, sys
try:
conn = pymysql.connect(host='localhost',
port=3306,
user='co302mc',
passwd='mafol597',
db='co302mc_bike_theft')
cur = conn.cursor()
cur.execute("CREATE TABLE bike (Id INT PRIMARY KEY AUTO_INCREMENT, Borough VARCHAR(20), Date DATE, Latitude DECIMAL(10, 8) NOT NULL, Longitude DECIMAL(10, 8) NOT NULL, Area VARCHAR(100), Investigaton VARCHAR(100))")
@mportocarrero
mportocarrero / Populating table - Mysql and Python
Last active January 2, 2016 14:39
Populating table - Mysql and Python I used the following command in MySql to load data inside of the table, but I could also have been used Python as both ways work. From now on, I will be using MySql to query and Python at the same time to output the results in a csv file. I will use PhpMyAdmin only to test my results, as it is a more visual so…
mysql> LOAD DATA LOCAL INFILE "Bikee.csv" INTO TABLE bike FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r' IGNORE 1 LINES;
Query OK, 358 rows affected (0.00 sec)
Records: 358 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from bike
-> ;
+---------------+------------+-------------+-------------+------------------------------------------------+-----------------------------------+
| Borough | Date | Latitude | Longitude | Area | Investigaton |
+---------------+------------+-------------+-------------+------------------------------------------------+-----------------------------------+
| Lewisham | 2013-10-01 | 51.44585000 | -0.02131800 | On or near Supermarket | No further action at this time |
@mportocarrero
mportocarrero / Query 1 - How many bikes were stolen in the boroughs between May and October 2013?
Last active January 2, 2016 14:39
Query 1 - How many bikes were stolen in the boroughs between May and October 2013? (MySql and Python) To answer this question I simply counted all the rows of the table, as each one represents one stolen bike. This first query was to make the readers familiar to which amount of I would be working from then on.
Query on MySql:
mysql> select count(*) from bike as total;
+----------+
| count(*) |
+----------+
| 321 |
+----------+
1 row in set (0.00 sec)
@mportocarrero
mportocarrero / Query 2 - How many bikes were stolen in each borough from May to October 2013?
Last active January 2, 2016 14:48
Query 2 - How many bikes were stolen in each borough from May to October 2013? This query enables the reader to perceive, in this period, which boroughs were the most and the least dangerous.
Query on MySql:
mysql> SELECT borough, COUNT(*) as count FROM bike GROUP BY Borough;
+---------------+-------+
| borough | count |
+---------------+-------+
| Croydon | 64 |
| Greenwich | 24 |
| Hackney | 51 |
| Lewisham | 24 |
@mportocarrero
mportocarrero / Query 4 - How many bikes were stolen in this period? (by month and borough)
Last active January 2, 2016 14:49
Query 4 - How many bikes were stolen in this period? (by month and borough) This query enables the readers to know what was the evolution of bikes stolen from May to October 2013.
Query on MySql:
mysql> SELECT Borough, Date, COUNT(*) FROM bike GROUP BY Borough, Date;
+---------------+------------+----------+
| Borough | Date | COUNT(*) |
+---------------+------------+----------+
| Croydon | 2013-05-01 | 5 |
| Croydon | 2013-06-01 | 20 |
| Croydon | 2013-07-01 | 10 |
| Croydon | 2013-08-01 | 4 |
@mportocarrero
mportocarrero / Query 3 - What is the Average, Minimum and Maximum number of bikes stolen from May to October 2013?
Last active January 2, 2016 17:09
Query 3 - What is the Average, Minimum and Maximum number of bikes stolen from May to October 2013? This query allows the reader to have a perception of the dimension of the bike theft in this period.
Query in MySql:
mysql> select avg(count), max(count), min(count) from (select borough, count(*) as count from bike group by borough) as c;
+------------+------------+------------+
| avg(count) | max(count) | min(count) |
+------------+------------+------------+
| 64.2000 | 158 | 24 |
+------------+------------+------------+
1 row in set (0.00 sec)
@mportocarrero
mportocarrero / Query 5 - What does the police normally do when a bike is stolen?
Last active January 2, 2016 17:09
Query 5 - What does the police normally do when a bike is stolen? This query counts the result of police investigation and allows the reader to see if most of the time the reported cases are solved or unsolved.
Query in MySql:
mysql> SELECT investigation, COUNT(*) FROM bike GROUP BY investigation;
+-----------------------------------+----------+
| investigation | COUNT(*) |
+-----------------------------------+----------+
| No further action at this time | 105 |
| Offender given a caution | 1 |
| Offender given community sentence | 1 |
| Offender sent to prison | 2 |
@mportocarrero
mportocarrero / Query 7 - How many bikes were stolen per month?
Last active January 2, 2016 17:09
Query 7 - How many bikes were stolen per month? This query allows the reader to see which months were the best and the worst in what concerns to bike theft.
Query in MySql:
mysql> select date, count(*) from bike group by date;
+------------+----------+
| date | count(*) |
+------------+----------+
| 2013-05-01 | 45 |
| 2013-06-01 | 68 |
| 2013-07-01 | 53 |
| 2013-08-01 | 40 |
@mportocarrero
mportocarrero / Creating another table - Python
Last active January 2, 2016 22:59
Creating another table - Python I decided to create another table called "cycling" to introduce data about the percentage of people that cycles to work everyday in the same boroughs. To create the table I used Python and checked it with My Sql.
Creating table in Python
import pymysql, sys
try:
conn = pymysql.connect(host='localhost',
port=3306,
user='co302mc',
passwd='mafol597',
db='co302mc_bike_theft')