Created
October 7, 2015 09:16
-
-
Save kurozumi/37196c0fd7d790512e20 to your computer and use it in GitHub Desktop.
【python】CSVファイルを読み込んで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
# 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