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. HDU 5500 Reorder the Books (水题)

    题意: 有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次? 思路: 如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽 ...

  2. 关于一些Spring MVC控制器的参数注解总结

    昨天同事问我控制器参数的注解的问题,我好久没那样写过,把参数和url一起设置,不过,今天我看了一些文章,查了一些资料,我尽可能的用我自己的理解方式来解释它吧! 1.@RequestParam绑定单个请 ...

  3. UVA 1616 Caravan Robbers 商队抢劫者(二分)

    x越大越难满足条件,二分,每次贪心的选区间判断是否合法.此题精度要求很高需要用long double,结果要输出分数,那么就枚举一下分母,然后求出分子,在判断一下和原来的数的误差. #include& ...

  4. UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)

    不难发现,每过一个小时,除了右下方的气球全都是蓝色以外,其他都和上一个小时的气球是一样的,所以是可以递推的.然后定义一类似个前缀和的东西f(k,i)表示k小时之后上面i行的红气球数.预处理出k小时的红 ...

  5. Python 多线程应用

    同步锁 import time import threading def subNum(): global num # print("ok") lock.acquire() # 加 ...

  6. LEETCODE60——第K个排列

    class Solution { public: string getPermutation(int n, int k) { '); vector<bool> flag(n, false) ...

  7. Windows 10+Ubuntu双系统修复Ubuntu启动引导

    U盘启动,联网 $ sudo su sudo add-apt add-apt-repository ppa:yannubuntu/boot-repair apt-get update apt-get ...

  8. 03Qt信号与槽(2)

    1. 元对象工具 ​ 元对象编译器 MOC(meta object compiler)对 C++ 文件中的类声明进行分析并产生用于初始化元对象的 C++ 代码,元对象包含全部信号和槽的名字及指向这些函 ...

  9. linux文件属性软硬链接知识

    链接的概念 在linux系统中,链接可分为两种:一种为硬链接,另一种为软链接或符号链接.在默认不带参数的情况下,执行ln命令创建的链接是硬链接. 如果使用ln  -s创建链接则为软链接,前面文件类型为 ...

  10. Verilog之语句位置

    1.if语句.case语句必须放在always过程语句块中. 2.verilog的系统函数比如:\(display/\)monitor必须放在initial 过程语句块中.这点尚为理解为何,但必须这样 ...