Tinydb 简明教程
TinyDB - Delete Data
如果您需要从 TinyDB 数据库中永久删除特定数据的话,您可以使用 remove() 方法来完成。与返回和更新数据类似,您首先需要创建一个 Query 类的实例来删除数据。您可以为此目的使用以下命令 -
In case you need to delete a particular set of data permanently from a TinyDB database, you can do so by using the remove() method. Like for reteriving and updating the data, you first need to create an instance of the Query class for deleting the data. You can use the following command for this purpose −
from tinydb import Query
Student = Query()
这里 Student 是我们数据库的名称。
Here, Student is the name of our database.
The remove() Method
以下是 remove() 方法的语法 -
Here is the syntax for the remove() method −
db.remove( Query() field regex )
remove() 方法接受可选条件以及可选文档 ID 列表。
The remove() method accepts both an optional condition as well as an optional list of documents IDs.
The student Database
对于本章的示例,我们将使用以下 student 数据库。
We will use the following student database, for the examples in this chapter.
[
{
"roll_number":1,
"st_name":"elen",
"mark":250,
"subject":"TinyDB",
"address":"delhi"
},
{
"roll_number":2,
"st_name":"Ram",
"mark":[
250,
280
],
"subject":[
"TinyDB",
"MySQL"
],
"address":"delhi"
},
{
"roll_number":3,
"st_name":"kevin",
"mark":[
180,
200
],
"subject":[
"oracle",
"sql"
],
"address":"keral"
},
{
"roll_number":4,
"st_name":"lakan",
"mark":200,
"subject":"MySQL",
"address":"mumbai"
},
{
"roll_number":5,
"st_name":"karan",
"mark":275,
"subject":"TinyDB",
"address":"benglore"
}
]
Example: Deleting a Single Row of Data
我们举一个例子来了解 remove() 方法。
Let’s take an example to understand the remove() method.
from tinydb import TinyDB, Query
student = Query()
db.remove(student.roll_number == 5)
上面的查询将删除学生学号为 5 的数据。它将返回被删除对象的 ID −
The above query will delete the data where the student’s roll number is 5. It will return the ID of the removed object −
[5]
现在,我们可以使用 all() 方法查看更新后的数据库。
Now, we can use the all() method to see the updated database.
db.all()
它将显示更新后数据库中的数据 −
It will display the data from the updated database −
[
{
"roll_number":1,
"st_name":"Adam",
"mark":250,
"subject":"TinyDB",
"address":"delhi"
},
{
"roll_number":2,
"st_name":"Ram",
"mark":[
250,
280
],
"subject":[
"TinyDB",
"MySQL"
],
"address":"delhi"
},
{
"roll_number":3,
"st_name":"kevin",
"mark":[
180,
200
],
"subject":[
"oracle",
"sql"
],
"address":"keral"
},
{
"roll_number":4,
"st_name":"lakan",
"mark":200,
"subject":"MySQL",
"address":"mumbai"
}
]
Example: Deleting Multiple Rows of Data
如果您一次想要删除多行,您可以按如下所示使用 remove() 方法 −
If you want to remove more than one row at a time, you can use the remove() method as follows −
from tinydb import TinyDB, Query
student = Query()
db.remove(student.roll_number > 2)
它将返回被删除对象的 ID:
It will return the IDs of the removed object:
[3,4]
使用 all() 方法查看更新后的数据库。
Use the all() method to see the updated database.
db.all()
它将显示更新后数据库中的数据 −
It will display the data from the updated database −
[
{
"roll_number":1,
"st_name":"Adam",
"mark":250,
"subject":"TinyDB",
"address":"delhi"
},
{
"roll_number":2,
"st_name":"Ram",
"mark":[
250,
280
],
"subject":[
"TinyDB",
"MySQL"
],
"address":"delhi"
}
]
Example: Deleting the Entire Data
如果您想要从数据库中删除所有数据,您可以按如下所示使用 truncate() 方法 −
If you want to remove all the data from a database, you can use the truncate() method as follows −
db.truncate()
接下来,使用 all() 方法查看更新后的数据库。
Next, use the all() method to see the updated database.
db.all()
它将显示一个空数据库作为 output −
It will show an empty database as the output −
[]