Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【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
You can’t perform that action at this time.