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的更多相关文章

  1. Oracle 拆分列为多行 Splitting string into multiple rows in Oracle

    =========================== The table is as follows: Name | Project | Error 108 test Err1, Err2, Err ...

  2. MySQL Crash Course #10# Chapter 19. Inserting Data

    INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...

  3. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...

  4. MyBatis(3.2.3) - Multiple results as a map

    If we have a mapped statement that returns multiple rows and we want the results in a HashMap with s ...

  5. MySQL Information Functions

    Table 12.18 Information Functions Name Description BENCHMARK() Repeatedly execute an expression CHAR ...

  6. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  7. ContentProvider官方教程(9)定义一个provider完整示例:实现方法,定义权限等

    Creating a Content Provider In this document Designing Data Storage Designing Content URIs Implement ...

  8. Oracle Database 11g express edition

    commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...

  9. windows 下使用 zip安装包安装MySQL 5.7

    以下内容参考官方文档:http://dev.mysql.com/doc/refman/5.7/en/windows-start-command-line.html 解压缩zip到D:\mysql-5. ...

随机推荐

  1. fpathconf

    http://pubs.opengroup.org/onlinepubs/009695399/functions/pathconf.html

  2. SQLServer 2012 Always on配置全过程

    AlwaysOn取数据库镜像和故障转移集群之长.AlwaysOn不再像故障转移集群那样需要共享磁盘,从而主副本和辅助副本可以更容易的部署到不同的地理位置:AlwaysOn还打破了镜像只能1对1的限制, ...

  3. SQL 事物回滚

    转自https://www.cnblogs.com/delphinet/archive/2010/08/17/1801424.html 第一种:   declare   @iErrorCount    ...

  4. C#语言基础 Main 函数中的输出输入

    C# 是一门面向对象的编程语言,保留了C  C++等等强大功能,但是它与 Java 非常相似,有许多强大的编程功能,它是微软(Microsoft)专门为.NET应用而开发的一门语言. 也就是人与计算机 ...

  5. 新手第一天学习 C#语言(进制互换)

    说起来我们对一些陌生或者未知的东西有一些恐惧感,但是又有一些期待,虽然我不确定自己能不能学会这门语言,但是我会尽自己最大的努力学. 我们第一天学的的内容呢,对大多数的人都知道,计算机的语言是二进制,但 ...

  6. HDU Rabbit and Grass 兔子和草 (Nim博弈)

    思路:简单Nim博弈,只需要将所给的数字全部进行异或,结果为0,则先手必败.否则必胜. #include <iostream> using namespace std; int main( ...

  7. setup命令

    setup——配置网络(centos) 命令所在路径:usr/bin/setup 示例1: # setup

  8. [中文翻译] ASP.NET 5 简介(Introducing ASP.NET 5,原作ScottGu 2015/2/23)

    本文出处  [中文翻译] ASP.NET 5 简介(Introducing ASP.NET 5,原作ScottGu 2015/2/23) 这是我的文章备份 http://www.dotblogs.co ...

  9. 一把剪刀看懂git reset 和它的三个参数

    都说git 命令难记且难懂,但是如果从立体的角度看待git与git管理的版本,那么一切都会明朗许多. 大多数的学习教程为了理解git,会绘制几个圆圈的串联,每个圆圈代表一个commit的版本,也就是从 ...

  10. Java 设计模式之中介者模式

    本文继续23种设计模式系列之中介者模式.   定义 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互.   角色 抽象中介者: ...