Python 数据访问教程

Python 数据访问主页

Python MySQL

Python MySQL 简介 Python MySQL 数据库连接 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入数据 Python MySQL 选择数据 Python MySQL Where 子句 Python MySQL 排序 Python MySQL 更新表 Python MySQL 删除数据 Python MySQL 删除表 Python MySQL Limit 子句 Python MySQL 连接 Python MySQL 游标对象

Python PostgreSQL

Python PostgreSQL 简介 Python PostgreSQL 数据库连接 Python PostgreSQL 创建数据库 Python PostgreSQL 创建表 Python PostgreSQL 插入数据 Python PostgreSQL 选择数据 Python PostgreSQL Where 子句 Python PostgreSQL 排序 Python PostgreSQL 更新表 Python PostgreSQL 删除数据 Python PostgreSQL 删除表 Python PostgreSQL Limit 子句 Python PostgreSQL 连接 Python PostgreSQL 游标对象

Python SQLite

Python SQLite 简介 Python SQLite 建立连接 Python SQLite 创建表 Python SQLite 插入数据 Python SQLite 选择数据 Python SQLite Where 子句 Python SQLite 排序 Python SQLite 更新表 Python SQLite 删除数据 Python SQLite 删除表 Python SQLite Limit 子句 Python SQLite 连接 Python SQLite 游标对象

Python MongoDB

Python MongoDB 简介 Python MongoDB 创建数据库 Python MongoDB 创建集合 Python MongoDB 插入文档 Python MongoDB 查找 Python MongoDB 查询 Python MongoDB 排序 Python MongoDB 删除文档 Python MongoDB 删除集合 Python MongoDB 更新 Python MongoDB Limit 子句

Python 数据访问资源

Python 数据访问 快速指南 Python 数据访问 有用资源 Python 数据访问 讨论


Python MySQL - 删除数据

要从 MySQL 表中删除记录,您需要使用 DELETE FROM 语句。要删除特定记录,您需要同时使用 WHERE 子句。

语法

以下是 MYSQL 中 DELETE 查询的语法 −

DELETE FROM table_name [WHERE 子句]

示例

假设我们在 MySQL 中创建了一个名为 EMPLOYEES 的表,如下所示 −

mysql> CREATE TABLE EMPLOYEE(
   FIRST_NAME CHAR(20) NOT NULL,
   LAST_NAME CHAR(20),
   AGE INT,
   SEX CHAR(1),
   INCOME FLOAT
);
Query OK, 0 rows affected (0.36 sec)

如果我们使用 INSERT 语句向其中插入 4 条记录,如下所示 −

mysql> INSERT INTO EMPLOYEE VALUES
   ('Krishna', 'Sharma', 19, 'M', 2000),
   ('Raj', 'Kandukuri', 20, 'M', 7000),
   ('Ramya', 'Ramapriya', 25, 'F', 5000),
   ('Mac', 'Mohan', 26, 'M', 2000);

以下 MySQL 语句删除 FIRST_NAME 为"Mac"的员工记录。

mysql> DELETE FROM EMPLOYEE WHERE FIRST_NAME = 'Mac';
Query OK, 1 row affected (0.12 sec)

如果您检索表的内容,您将只能看到 3 条记录,因为我们已删除了一条记录。

mysql> select * from EMPLOYEE;
+------------+-----------+------+------+--------+
| FIRST_NAME | LAST_NAME | AGE  | SEX  | INCOME |
+------------+-----------+------+------+--------+
| Krishna    | Sharma    | 20   | M    | 2000   |
| Raj        | Kandukuri | 21   | M    | 7000   |
| Ramya      | Ramapriya | 25   | F    | 5000   |
+------------+-----------+------+------+--------+
3 rows in set (0.00 sec)

如果执行不带 WHERE 子句的 DELETE 语句,则指定表中的所有记录都将被删除。

mysql> DELETE FROM EMPLOYEE;
Query OK, 3 rows affected (0.09 sec)

如果你检索表格的内容,你将得到一个空集,如下所示 −

mysql> select * from EMPLOYEE;
Empty set (0.00 sec)

使用 python 删除表中的记录

当您想要从数据库中删除一些记录时,需要执行 DELETE 操作。

要删除表中的记录 −

  • 导入 mysql.connector 包。

  • 使用 mysql.connector.connect() 方法创建连接对象,将用户名、密码、主机(可选默认值:localhost)和数据库(可选)作为参数传递给它。

  • 通过在上面创建的连接对象上调用 cursor() 方法创建游标对象。

  • 然后,通过将 DELETE 语句作为参数传递给 execute() 来执行该语句方法。

示例

以下程序删除 EMPLOYEE 中所有 AGE 大于 20 的记录 −

import mysql.connector

#建立连接
conn = mysql.connector.connect(
   user='root', password='password', host='127.0.0.1', database='mydb')

#使用 cursor() 方法创建游标对象
cursor = conn.cursor()

#检索单行
print("Contents of the table: ")
cursor.execute("SELECT * from EMPLOYEE")
print(cursor.fetchall())

#准备删除记录的查询
sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (25)

try:
   # 执行 SQL 命令
   cursor.execute(sql)
   
   # 在数据库中提交你的更改
   conn.commit()
except:
   # 如果出现任何错误,请回滚
   conn.rollback()

#Retrieving data
print("Contents of the table after delete operation ")
cursor.execute("SELECT * from EMPLOYEE")
print(cursor.fetchall())

#关闭连接
conn.close()

输出

Contents of the table:
[('Krishna', 'Sharma', 22, 'M', 2000.0), 
   ('Raj', 'Kandukuri', 23, 'M', 7000.0), 
   ('Ramya', 'Ramapriya', 26, 'F', 5000.0), 
   ('Mac', 'Mohan', 20, 'M', 2000.0), 
   ('Ramya', 'Rama priya', 27, 'F', 9000.0)]

Contents of the table after delete operation:
[('Krishna', 'Sharma', 22, 'M', 2000.0), 
   ('Raj', 'Kandukuri', 23, 'M', 7000.0), 
   ('Mac', 'Mohan', 20, 'M', 2000.0)]