- Python SQLite Tutorial
- Python SQLite - Home
- Python SQLite - Introduction
- Python SQLite - Establishing Connection
- Python SQLite - Create Table
- Python SQLite - Insert Data
- Python SQLite - Select Data
- Python SQLite - Where Clause
- Python SQLite - Order By
- Python SQLite - Update Table
- Python SQLite - Delete Data
- Python SQLite - Drop Table
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - Cursor Object
- Python SQLite Useful Resources
- Python SQLite - Quick Guide
- Python SQLite - Useful Resources
- Python SQLite - Discussion
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 条记录,因为我们已经删除了 1 条记录。
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>
由于您已删除所有记录,因此如果您尝试检索 CRICKETERS 表的内容,使用 SELECT 语句您将得到一个空结果集,如下所示 -
sqlite> SELECT * FROM CRICKETERS; sqlite>
使用Python删除数据
将记录添加到 SQLite 数据库中的现有表 -
导入sqlite3包。
通过将数据库名称作为参数传递给它,使用connect()方法创建连接对象。
Cursor ()方法返回一个游标对象,您可以使用它与 SQLite3 进行通信。通过在(上面创建的)Connection 对象上调用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) ]