Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created October 7, 2015 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/37196c0fd7d790512e20 to your computer and use it in GitHub Desktop.
Save kurozumi/37196c0fd7d790512e20 to your computer and use it in GitHub Desktop.
【python】CSVファイルを読み込んでMySQLでデータベースに保存
# coding:utf-8
import csv
import MySQLdb
connection = MySQLdb.connect(db="databse",user="username",passwd="password")
cursor=connection.cursor()
f = open("csvfile.csv", "r")
reader = csv.reader(f)
header = next(reader)
for row in reader:
sql = "INSERT IGNORE INTO table_name values(%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(sql, (row[0], row[1], row[2],row[3],row[4],row[5],row[6]))
f.close()
connection.commit()
cursor.close()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment