📅  最后修改于: 2020-11-07 07:55:51             🧑  作者: Mango
您可以使用PostgreSQL数据库的DELETE FROM语句删除现有表中的记录。要删除特定记录,您需要与其一起使用WHERE子句。
以下是PostgreSQL中DELETE查询的语法-
DELETE FROM table_name [WHERE Clause]
假设我们使用以下查询创建了一个名为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
以下语句删除姓“ 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
由于已删除所有记录,因此,如果尝试检索CRICKETERS表的内容,则使用SELECT语句,您将获得一个空结果集,如下所示-
postgres=# SELECT * FROM CRICKETERS;
first_name | last_name | age | place_of_birth | country
------------+-----------+-----+----------------+---------
(0 rows)
psycopg2的游标类提供了一个名为execute()方法的方法。此方法接受查询作为参数并执行它。
因此,要使用Python将数据插入PostgreSQL中的表中-
导入psycopg2软件包。
通过将用户名,密码,主机(可选的默认值:localhost)和数据库(可选)作为参数传递给connect()方法,创建连接对象。
通过将false设置为属性autocommit的值来关闭自动提交模式。
psycopg2库的Connection类的cursor()方法返回一个游标对象。使用此方法创建一个游标对象。
然后,通过将DELETE语句作为参数传递给execute()方法来执行它。
以下Python代码删除年龄值大于25的EMPLOYEE表的记录-
import psycopg2
#establishing the connection
conn = psycopg2.connect(
database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)
#Setting auto commit false
conn.autocommit = True
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Retrieving contents of the table
print("Contents of the table: ")
cursor.execute('''SELECT * from EMPLOYEE''')
print(cursor.fetchall())
#Deleting records
cursor.execute('''DELETE FROM EMPLOYEE WHERE AGE > 25''')
#Retrieving data after delete
print("Contents of the table after delete operation ")
cursor.execute("SELECT * from EMPLOYEE")
print(cursor.fetchall())
#Commit your changes in the database
conn.commit()
#Closing the connection
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)
]