Created
January 18, 2014 18:52
-
-
Save skyowen/8494584 to your computer and use it in GitHub Desktop.
This file contains 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
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