
- Python SQLite 教程
- Python SQLite - 首頁
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 選擇資料
- Python SQLite - WHERE 子句
- Python SQLite - ORDER BY
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - LIMIT
- Python SQLite - JOIN
- Python SQLite - 遊標物件
- Python SQLite 有用資源
- Python SQLite - 快速指南
- Python SQLite - 有用資源
- Python SQLite - 討論
Python SQLite - 刪除資料
要從 SQLite 表中刪除記錄,您需要使用 DELETE FROM 語句。要刪除特定記錄,您需要與之一起使用 WHERE 子句。
語法
以下是 SQLite 中 DELETE 查詢的語法:
DELETE FROM table_name [WHERE Clause]
示例
假設我們使用以下查詢建立了一個名為 CRICKETERS 的表:
sqlite> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqlite>
如果我們使用 INSERT 語句向其中插入了 5 條記錄,如下所示:
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India'); sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica'); sqlite> insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka'); sqlite> insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India'); sqlite> insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India'); sqlite>
以下語句刪除姓氏為“Sangakkara”的板球運動員的記錄。
sqlite> DELETE FROM CRICKETERS WHERE LAST_NAME = 'Sangakkara'; sqlite>
如果您使用 SELECT 語句檢索表的內容,您只能看到 4 條記錄,因為我們刪除了一條。
sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- -------- Shikhar Dhawan 46 Delhi India Jonathan Trott 39 CapeTown SouthAfrica Virat Kohli 31 Delhi India Rohit Sharma 33 Nagpur India sqlite>
如果您在沒有 WHERE 子句的情況下執行 DELETE FROM 語句,則將刪除指定表中的所有記錄。
sqlite> DELETE FROM CRICKETERS; sqlite>
由於您已刪除所有記錄,因此如果您嘗試使用 SELECT 語句檢索 CRICKETERS 表的內容,您將獲得一個空的結果集,如下所示:
sqlite> SELECT * FROM CRICKETERS; sqlite>
使用 Python 刪除資料
要向 SQLite 資料庫中的現有表新增記錄:
匯入 sqlite3 包。
使用 connect() 方法建立一個連線物件,並將資料庫名稱作為引數傳遞給它。
cursor() 方法返回一個遊標物件,您可以使用它與 SQLite3 通訊。透過在(上面建立的)連線物件上呼叫 cursor() 物件來建立一個遊標物件。
然後,透過將 DELETE 語句作為引數傳遞給它,在遊標物件上呼叫 execute() 方法。
示例
以下 Python 示例從 EMPLOYEE 表中刪除年齡值大於 25 的記錄。
import sqlite3 #Connecting to sqlite conn = sqlite3.connect('example.db') #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), ('Vinay', 'Battacharya', 21, 'M', 6000.0), ('Sharukh', 'Sheik', 26, 'M', 8300.0), ('Sarmista', 'Sharma', 26, 'F', 10000.0), ('Tripthi', 'Mishra', 24, 'F', 6000.0) ] Contents of the table after delete operation [ ('Vinay', 'Battacharya', 21, 'M', 6000.0), ('Tripthi', 'Mishra', 24, 'F', 6000.0) ]
廣告