Skip to content

Instantly share code, notes, and snippets.

@rafaelnovello
Created February 1, 2013 19:53
Show Gist options
  • Save rafaelnovello/4693651 to your computer and use it in GitHub Desktop.
Save rafaelnovello/4693651 to your computer and use it in GitHub Desktop.
Carregar arquivo csv no MySQL
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