码迷,mamicode.com
首页 > 其他好文 > 详细

[转]how to inserting multiple rows in one step

时间:2018-02-26 20:33:04      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:tab   within   com   contain   let   mysqld   count   statement   arguments   

To insert multiple rows in the table use executemany() method of cursor object.

Syntax:
cursor_object.executemany(statement, arguments)

  • statement: string containing the query to execute.
  • arguments: a sequence containing values to use within insert statement.

Let’s take an example.

from __future__ import print_function
 
import MySQLdb as my
 
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
 
cursor = db.cursor()
name = "Some new city"
 
country_code = 'SNC'
 
district = 'Someyork'
 
population = 10008
 
data = [
('city 1', 'MAC', 'distrct 1', 16822),
('city 2', 'PSE', 'distrct 2', 15642),
('city 3', 'ZWE', 'distrct 3', 11642),
('city 4', 'USA', 'distrct 4', 14612),
('city 5', 'USA', 'distrct 5', 17672),
]
 
sql = "insert into city(name, countrycode, district, population) 
VALUES(%s, %s, %s, %s)"
 
number_of_rows = cursor.executemany(sql, data)
db.commit()
 
db.close()

[转]how to inserting multiple rows in one step

标签:tab   within   com   contain   let   mysqld   count   statement   arguments   

原文地址:https://www.cnblogs.com/everfight/p/execute_manysql.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!