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 PostgreSQL - Delete 删除数据

您可以使用 PostgreSQL 数据库的 DELETE FROM 语句删除现有表中的记录。要删除特定记录,您需要与其一起使用 WHERE 子句。

语法

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

DELETE FROM table_name [WHERE 子句]

示例

假设我们使用以下查询创建了一个名为 CRICKETERS 的表 −

postgres=# CREATE TABLE CRICKETERS ( 
   First_Name VARCHAR(255), Last_Name VARCHAR(255), 
   Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255)
);
CREATE TABLE
postgres=#

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

postgres=# insert into CRICKETERS values ('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into CRICKETERS values ('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
INSERT 0 1
postgres=# insert into CRICKETERS values ('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
INSERT 0 1
postgres=# insert into CRICKETERS values ('Virat', 'Kohli', 30, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into CRICKETERS values ('Rohit', 'Sharma', 32, 'Nagpur', 'India');
INSERT 0 1

以下语句删除 LAST_NAME 为"Sangakkara"的运动员的记录。 −

postgres=# DELETE FROM CRICKETERS WHERE LAST_NAME = 'Sangakkara';
DELETE 1

如果使用 SELECT 语句检索表的内容,则只能看到 4 条记录,因为我们已删除了一条。

postgres=# SELECT * FROM CRICKETERS;
first_name  | last_name | age | place_of_birth | country
------------+-----------+-----+----------------+-------------
Jonathan    | Trott     | 39  | CapeTown       | SouthAfrica
Virat       | Kohli     | 31  | Delhi          | India
Rohit       | Sharma    | 33  | Nagpur         | India
Shikhar     | Dhawan    | 46  | Delhi          | India
(4 rows)

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

postgres=# DELETE FROM CRICKETERS;
DELETE 4

由于您已删除所有记录,如果您尝试使用 SELECT 语句检索 CRICKETERS 表的内容,您将获得一个空结果集,如下所示 −

postgres=# SELECT * FROM CRICKETERS;
first_name  | last_name | age | place_of_birth | country
------------+-----------+-----+----------------+---------
(0 rows)

使用 python 删除数据

psycopg2 的 cursor 类提供了一个名为 execute() 方法的方法。此方法接受查询作为参数并执行它。

因此,要使用 python − 将数据插入 PostgreSQL 中的表中

  • 导入 psycopg2 包。

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

  • 通过将属性 autocommit 的值设置为 false 来关闭自动提交模式。

  • psycopg2 库的 Connection 类的 cursor() 方法返回一个游标对象。使用此方法创建一个游标对象。

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

示例

以下 Python 代码删除 EMPLOYEE 表中年龄值大于 25 的记录 −

import psycopg2

#建立连接
conn = psycopg2.connect(
   database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)

#设置自动提交为 false
conn.autocommit = True

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

#检索表的内容
print("Contents of the table: ")
cursor.execute('''SELECT * from EMPLOYEE''')
print(cursor.fetchall())

#删除记录
cursor.execute('''DELETE FROM EMPLOYEE WHERE AGE > 25''')

#删除后检索数据
print("Contents of the table after delete operation ")
cursor.execute("SELECT * from EMPLOYEE")
print(cursor.fetchall())

#在数据库中提交你的更改
conn.commit()

#关闭连接
conn.close()

输出

Contents of the table:
[('Ramya', 'Rama priya', 27, 'F', 9000.0), 
   ('Sarmista', 'Sharma', 26, 'F', 10000.0), 
   ('Tripthi', 'Mishra', 24, 'F', 6000.0), 
   ('Vinay', 'Battacharya', 21, 'M', 6000.0), 
   ('Sharukh', 'Sheik', 26, 'M', 8300.0)]
Contents of the table after delete operation:
[('Tripthi', 'Mishra', 24, 'F', 6000.0), 
   ('Vinay', 'Battacharya', 21, 'M', 6000.0)]