插入表

要把记录插入到MySQL中的表中,使用“INSERT INTO”语句。

示例

在“customers”表中插入一条记录:

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码",
database="mydatabase"
) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "条记录已插入")

注意: 调用mydb.commit()语句提交修改,否则修改不会生效。

插入多行

要将多行插入到表中,使用executemany()方法。

executemany()方法的第二个参数是一个元组列表,包含了要插入的数据:

示例

填写“客户”表格:

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码",
database="mydatabase"
) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
] mycursor.executemany(sql, val) mydb.commit() print(mycursor.rowcount, " 条已经插入")

获取插入行的ID

可以通过查询cursor对象,获得刚才插入行的id。

注意: 如果插入多行,则返回最后插入行的id。

示例

插入一行,并返回ID:

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="你的用户名",
passwd="你的密码",
database="mydatabase"
) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("Michelle", "Blue Village")
mycursor.execute(sql, val) mydb.commit() print("1 条记录插入, ID:", mycursor.lastrowid)

Python MySQL 插入表的更多相关文章

  1. Python MySQL 删除表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  2. Python MySQL 创建表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  3. python MySQL 插入Elasticsearch

    一.需求分析 注意: 本环境使用 elasticsearch 7.0版本开发,切勿低于此版本 mysql 表结构 有一张表,记录的数据特别的多,需要将7天前的记录,插入到Elasticsearch中, ...

  4. mysql插入表数据中文乱码问题解决方案

    一.问题 开发中遇到将其它数据库数据插入到mysql数据库表中一直会报类似如下错误: Incorrect string value: '\xE6\x88\x91' for column 'name' ...

  5. python Mysql 库表

    Mysql 库表    创建 学生信息库表  学生成绩 库表

  6. PYTHON MYSQL 的表创建和插入

    import mysql.connector cnx = mysql.connector.connect(user='xx',password='xx++.',host='139.107.11.166 ...

  7. python mysql 单表查询 多表查询

    一.外键 变种: 三种关系: 多对一 站在左表的角度: (1)一个员工 能不能在 多个部门? 不成立 (2)多个员工 能不能在 一个部门? 成立 只要有一个条件成立:多 对 一或者是1对多 如果两个条 ...

  8. python mysql创建表

    表设计 表:student 字段名 类型 是否为空 主键 描述 StdID int 否 是 学生ID StdName varchar(100) 否 学生姓名 Gender enum('M','F') ...

  9. python mysql插入中文乱码

    # "INSERT INTO" 语句sql = "INSERT INTO sites (name, url, status, enable) VALUES (%s, %s ...

随机推荐

  1. 《React后台管理系统实战 :二》antd左导航:cmd批量创建子/目录、用antd进行页面布局、分离左导航为单独组件、子路由、动态写左导航、css样式相对陷阱

    一.admin页面布局及路由创建 0)cmd批量创建目录及子目录 //创建各个目录,及charts和子目录bar md home category product role user charts\b ...

  2. 吴裕雄--天生自然HADOOP学习笔记:hadoop集群实现PageRank算法实验报告

    实验课程名称:大数据处理技术 实验项目名称:hadoop集群实现PageRank算法 实验类型:综合性 实验日期:2018年 6 月4日-6月14日 学生姓名 吴裕雄 学号 15210120331 班 ...

  3. Day9 - K - Yue Fei's Battle HDU - 5136

    Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in t ...

  4. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  5. Xeon 第一次训练赛 苏州大学ICPC集训队新生赛第二场(同步赛) [Cloned]

    A.给出一个字符串,求出连续的权值递增和,断开以后权值重新计数,水题 #include<iostream> #include<string> #include<cmath ...

  6. 吴裕雄--天生自然JAVA数据库编程:JDBC操作步骤及数据库连接操作

    public class ConnectionDemo01{ // 定义MySQL的数据库驱动程序 public static final String DBDRIVER = "org.gj ...

  7. POJ 3292:Semi-prime H-numbers 筛选数

    Semi-prime H-numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8216   Accepted: 3 ...

  8. mysql批量插入更新操作

    //添加关联赠品(确定) public function addGiveGoods($ids,$child,$parent_sku_no){ $license=new LicenseModel(); ...

  9. 调用dos

    两个方法 function RunDosCommand(Command: string): string; var hReadPipe: THandle; hWritePipe: THandle; S ...

  10. 解决使用xampp无法通过ip访问的问题

    本地能通过127.0.0.1或者localhost访问,无法通过外网地址访问: win解决防火墙问题: 1.进入控制面板 -> 系统和安全 -> 2.window防火墙 - 允许其他程序通 ...