Forked from skyowen/Import CSV into MySQL with Python
Created
September 7, 2017 07:03
-
-
Save arvind-india/630477b61526397202df68892c1bce0a 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 | |
# open the connection to the MySQL server. | |
# using MySQLdb | |
mydb = MySQLdb.connect(host='igor.gold.ac.uk', user='co304so', passwd='*********', db='co304so_LondonCrime') | |
cursor = mydb.cursor() | |
csv_data = csv.reader(file('1.csv')) | |
# execute and insert the csv into the database. | |
for row in csv_data: | |
cursor.execute('INSERT INTO AprilNov2013(Date,Westminster,HACKNEY,Tower_Hamlets)''VALUES(%s, %s, %s, %s)',row) | |
print row | |
#close the connection to the database. | |
mydb.commit() | |
cursor.close() | |
print "CSV has been imported into the database" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment