Python PostgreSQL - 刪除資料



您可以使用 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

由於您已刪除所有記錄,如果您嘗試使用 SELECT 語句檢索 CRICKETERS 表的內容,您將得到一個空的結果集,如下所示:

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

使用 Python 刪除資料

psycopg2 的遊標類提供了一個名為 execute() 的方法。此方法接受查詢作為引數並執行它。

因此,要使用 python 將資料插入 PostgreSQL 表中:

  • 匯入psycopg2 包。

  • 使用connect()方法建立一個連線物件,將使用者名稱、密碼、主機(可選,預設為 localhost)和資料庫(可選)作為引數傳遞給它。

  • 透過將 false 設定為屬性autocommit的值來關閉自動提交模式。

  • psycopg2 庫的Connection類的cursor()方法返回一個遊標物件。使用此方法建立一個遊標物件。

  • 然後,透過將其作為引數傳遞給 execute() 方法來執行 DELETE 語句。

示例

以下 Python 程式碼刪除 EMPLOYEE 表中年齡值大於 25 的記錄:

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)
]
廣告