Tinydb 简明教程

TinyDB - Querying

TinyDB 有一系列丰富的查询。我们有构造查询的方法:第一种方法类似于 ORM 工具的语法,第二种方法是使用“Where”子句的传统方法。

TinyDB has a rich set of queries. We have ways to construct queries: the first way resembles the syntax of ORM tools and the second is the traditional way of using the 'Where' clause.

在本章节中,我们来理解在 TinyDB 数据库中构造查询的这两种方法。

In this chapter, let’s understand these two ways of constructing a query in a TinyDB database.

The First Method: Importing a Query

第一种方法类似于 ORM 工具的语法,其中我们首先需要在命令提示符中导入查询。导入后,我们可以使用查询对象操作 TinyDB 数据库。 syntax 如下所示 −

The first method resembles the syntax of ORM tools in which first we need to import the query in the command prompt. After importing, we can use the query object to operate the TinyDB database. The syntax is given below −

from tinydb import Query
student = Query()

这里,“student”是我们的数据库名称。对于这些示例,我们将使用以下 student 数据库。

Here, 'student' is the name of our database. For the examples, we will be using the following 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"
   }
]

Example

以下是查询从 student 数据库中获取数据,其中学生的 roll_no 小于 3 −

Following is the query to retereive the data from the student database where the roll_no of the students are less than 3 −

>>> db.search(Query().roll_number < 3)

Or,

Or,

>>> student = Query()
>>> db.search(student.roll_number < 3)

上面的搜索查询将产生以下结果 −

The above search query will produce the following result −

[
   {
      "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"
   }
]

有时文件名不是有效的 Python 标识符。在这种情况下,我们将无法访问该字段。对于这种情况,我们需要按如下所示切换到 dict access notation

Sometimes the file name is not a valid Python identifier. In that case, we would not be able to access that field. For such cases, we need to switch to dict access notation as follows −

student = Query();

# Invalid Python syntax
db.search(student.security-code == 'ABCD')

# Use the following dict access notation
db.search(student['security-code'] == 'ABCD')

The Second Method: Using the "where" Clause

第二种方法是构造使用“where”子句的查询的传统方法。 syntax 如下所示 −

The second way is the traditional way of constructing queries that uses the "where" clause. The syntax is given below −

from tinydb import where
db.search(where('field') == 'value')

Example

TinyDB 的 where 用于字段 subject 的子句 −

TinyDB "where" clause for the subject field −

db.search(where('subject') == 'MySQL')

上述查询会生成以下 output

The above query will produce the following output

[{
      "roll_number":4,
      "st_name":"lakan",
      "mark":200,
      "subject":"MySQL",
      "address":"mumbai"
}]