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. 机器学习&深度学习资料分享

    感谢:https://github.com/ty4z2008/Qix/blob/master/dl.md <Brief History of Machine Learning> 介绍:这是 ...

  2. MovieReview—Black Panther(黑豹)

    Justice & Evil   The night before the night, i saw the latest movie in the Marvel series at JiaH ...

  3. URAL 2027 URCAPL, Episode 1 (模拟)

    题意:给你一个HxW的矩阵,每个点是一个指令,根据指令进行一系列操作. 题解:模拟 #include<cstdio> #include<algorithm> using nam ...

  4. cv2.minAreaRect() 生成最小外接矩形

    简介   使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是所要求最小外接矩形的点集数组或向量,这个点集不定个数.   cv2 ...

  5. 掘金 里面 写文章 带目录的时候 用#(空格)标题 后面用## title,一个页面只有一个H1

    掘金 里面 写文章 带目录的时候 用#(空格)标题 后面用## title,一个页面只有一个H1

  6. Mysql查询指定用户并列排名 类似rank函数

    SELECT total.* FROM ( SELECT obj.uid, obj.score, CASE WHEN @rowtotal = obj.score THEN @rownum WHEN @ ...

  7. C语言结构体和共用体_07

    概述 定义结构体类型变量的方法 结构体变量的引用 结构体变量的初始化 结构体数组 指向结构体类型数据的指针 用指针处理链表 共用体 枚举类型 用 typedef定义类型  概述 定义一个结构体的一般形 ...

  8. 《effective c++》问题总结

    04 确定对象被使用前已先被初始化 1.static/heap/stack对象 2.trivial对象 3.模板隐式具现化 implicit template instantiations 4.Sin ...

  9. Java语言中的异常处理

    Java语言中的异常处理包括声明异常.抛出异常.捕获异常和处理异常四个环节.   throw用于抛出异常.   throws关键字可以在方法上声明该方法要抛出的异常,然后在方法内部通过throw抛出异 ...

  10. 如何下载并安装 robomongo 到Ubuntu 系统

    官网下载软件,https://robomongo.org/download wget https://download.robomongo.org/1.2.1/linux/robo3t-1.2.1-l ...