Skip to content

Instantly share code, notes, and snippets.

@skyowen
Created January 18, 2014 18:52
Show Gist options
  • Save skyowen/8494584 to your computer and use it in GitHub Desktop.
Save skyowen/8494584 to your computer and use it in GitHub Desktop.
import csv
import MySQLdb
mydb = MySQLdb.connect(host='igor.gold.ac.uk',
user='co304so',
passwd='**********',
db='co304so_LondonCrime')
cursor = mydb.cursor()
## query
query = ("SELECT Date,London FROM Theft_Person_Years ORDER BY London DESC")
cursor.execute(query)
### write to csv file
csv_writer = csv.writer(open("Total_Thefts_Person_London_Desc.csv", "wt")) # create csv
csv_writer.writerow([i[0] for i in cursor.description]) # write headers
csv_writer.writerows(cursor) # write records
del csv_writer # close csv file
cursor.close()
mydb.close()
print "Query executed."
print "Wrote %s rows to csv." % cursor.rowcount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment