MySQLdb使用批量插入executemany方法插入mysql

python的MySQLdb庫能夠使用批量操做executemany,進行多行插入。python

好比向user表(username,salt,pwd)插入數據,具體的sql語句以下:sql

sql = 'INSERT INTO 表名 VALUES(%s,%s,%s)'  #無論什麼類型,統一使用%s做爲佔位符
param = ((username1, salt1, pwd1), (username2, salt2, pwd2), (username3, salt3, pwd3))  #對應的param是一個tuple或者list
n=cursor.executemany(sql,param)

以前使用execute方法循環寫入數據,表字段多的時候,每秒有時只能寫入幾條,而executemany方法一次性所有提交,1w條數據只用了不到1s,極大提高了性能。性能

相關文章
相關標籤/搜索