Created
February 1, 2013 19:53
-
-
Save rafaelnovello/4693651 to your computer and use it in GitHub Desktop.
Carregar arquivo csv no MySQL
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='', | |
user='root', | |
passwd='', | |
db='') | |
cursor = mydb.cursor() | |
csv_data = csv.reader( | |
file('file.csv')) | |
for index, row in enumerate(csv_data): | |
if index != 0: | |
cursor.execute("INSERT INTO example(Advertiser, Site, Banner," \ | |
"Date, Impressions, Clicks, Sessions, KPI)" \ | |
"VALUES(%s, %s, %s, %s, %s, %s, %s, %s)", | |
row) | |
#close the connection to the database. | |
mydb.commit() | |
cursor.close() | |
print "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment