Tinydb 简明教程
TinyDB - Retrieve Data
有许多方法可以帮助您从 TinyDB 数据库中检索数据。但是要使用这些方法,您首先需要按如下所示创建 Query 类的实例 −
There are numerous ways with the help of which you can retrieve data from a TinyDB database. But to use those ways, you first need to create an instance of the Query class as follows −
from tinydb import Query
Student = Query()
此处, Student 是数据库的名称。
Here, Student is the name of the database.
让我们探索从 TinyDB 数据库检索信息的各种方式。
Let’s check the various ways to retrieve the information from a TinyDB database.
Data Retrieval Using the Search() Method
search() 方法(顾名思义)返回与我们提供的查询匹配的项目列表,否则它将返回一个空列表。
The search() method, as its name implies, returns the list of items that mathches the query we provided, otherwise it will return an empty list.
对于此示例和其他示例,我们将使用以下 student 数据库数据 -
For this and other examples, we will be using the following student database data −
[
{
"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 来理解 search() 方法 -
Let’s take an example to understand the search() method −
from tinydb import TinyDB, Query
db = TinyDB("leekha.json")
student = Query()
db.search(student.subject == 'TinyDB' )
上述查询将从 student 数据库中检索以下 output -
The above query will retrieve the the following output from the student database −
[
{
"roll_number":1,
"st_name":"elen",
"mark":250,
"subject":"TinyDB",
"address":"delhi"
},
{
"roll_number":5,
"st_name":"karan",
"mark":275,
"subject":"TinyDB",
"address":"benglore"
}
]
Data Retrieval Using the get() Method
与 search() 方法相反,get() 方法仅返回一个匹配的文档。否则,它将返回 None。例如,让我们采用以下代码 -
As opposed to the search() method, the get() method returns only one matching document. It will return None, otherwise. For example, let’s take the following code −
from tinydb import TinyDB, Query
student = Query()
db.get(student.subject == 'TinyDB' )
上述查询将从 student 数据库中检索以下数据。
The above query will retrieve the following data from the student database.
[{'roll_number': 1, 'st_name': 'elen', 'mark': 250, 'subject': 'TinyDB', 'address': 'delhi'}]
Data Retrieval using the all() Method
all() 方法返回数据库中的所有文档。例如,
The all() method returns all the documents in the database. For example,
db.all()
它将从 student 数据库中检索全部数据。
It will retrieve the entire data from the student database.
[
{
"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"
}
]