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