[转]how to inserting multiple rows in one step

Posted everfight的成长之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转]how to inserting multiple rows in one step相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[转]How to Clean the Global Assembly Cache

[转]nopCommerce Widgets and How to Create One

[转]How to add new table in NopCommerce

[转]SQL - Create XML - How to set Unicode UTF-8

[转]Android How to Download and Make Volley.jar

How I explained OOD to my wife(转)