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. get_user

    Name get_user --    Get a simple variable from user space. Synopsis get_user ( x, ptr); Arguments x ...

  2. SQLServer 2012 Always on配置全过程

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

  3. python 之网页解析器

    一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出“ ...

  4. POJ 3311 Hie with the Pie (状压DP)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  5. Kunernetes集群架构与组件

    架构如图: master节点:主要是集群控制面板的功能,来管理整个集群,包括全局的角色,调度,都是在master节点进行控制 有三个组件: API Server:  是 k8s提供的一个统一入口,它是 ...

  6. Stream great concerts wherever you are

    This time of year, we take stock of what we're thankful for — and above all else, we’re thankful for ...

  7. cocos2x (c++/lua) spine 文件的预加载

    在之前,笔者写过一编博客,通过lua在加载场景加载spineAnimation动画精灵,保存在table中,然后在游戏中创建动画精灵时,提取加载好的spineAnimaiton中的 spSkeleto ...

  8. console.log与console.dir的区别

    今天学习promise的时候看到了console.dir这个方法,感到很好奇,查了以下感觉又长知识了 在Chrome中,控制台对象定义了两个似乎做同样事情的方法: console.log() cons ...

  9. Factorialize a Number-freecodecamp算法题目

    Factorialize a Number(计算一个整数的阶乘) 要求 给定一个整数,求其阶乘(用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积) 思路 确定乘的次数 用for循环进行累 ...

  10. Create & use FTP service on Ubuntu(在Ubuntu上搭建并使用FTP服务)

    Check if the FTP service has been installed.(检查是否已安装)   Vsftpd --version  If it has not install,Pres ...