使用Python快速(批量)插入MySQL [英] Speeding (Bulk) Insert into MySQL with Python
本文介绍了使用Python快速(批量)插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在部署一个应用程序以使用一些.csv数据.我想将它们复制到MySQL表中.在stackoverflow用户的一些帮助下,我编写了下面的代码:
I'm deploying an application to consume some .csv data. I want to copy them to a MySQL table. With some help from the stackoverflow users I wrote the code bellow:
import csv
import MySQLdb
db = MySQLdb.connect( host = "dbname.description.host.com",
user = "user",
passwd = "key",
db = "dbname")
cursor = db.cursor()
query = 'INSERT INTO table_name(column,column_1,column_2,column_3)
VALUES(%s, %s, %s, %s)'
csv_data = csv.reader(file('file_name'))
for row in csv_data:
cursor.execute(query,row)
db.commit()
cursor.close()
问题是,当前该过程太慢了,我需要加快速度.
The problem is, currently, the process is so slow and I need to speed the things up.
Thx
推荐答案
您可以使用executemany
如下分批处理作业
you can use executemany
to batch the job as follows
import csv
import MySQLdb
db = MySQLdb.connect( host = "dbname.description.host.com",
user = "user",
passwd = "key",
db = "dbname")
cursor = db.cursor()
query = 'INSERT INTO table_name(column,column_1,column_2,column_3)
VALUES(%s, %s, %s, %s)'
csv_data = csv.reader(file('file_name'))
my_data = []
for row in csv_data:
my_data.append(tuple(row))
cursor.executemany(query, my_data)
cursor.close()
这篇关于使用Python快速(批量)插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文