Tinydb 简明教程

TinyDB - Update Data

TinyDB 可以以多种格式存储数据,我们可以使用多种方法轻松地重新访问存储的数据。但有时,我们需要更新数据,我们可以使用 update() 方法来更新数据。

TinyDB can store data in many formats and we can easily reterive the stored data using various methods. But sometimes, we need to update the data, for which we can use the update() method.

要更新数据库,我们首先需要创建 Query 类的实例。您可以为此目的使用以下命令-

For updating the database, we first need to create an instance of the Query class. 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 update() Method

下面是 update() 方法的语法版本-

Here is the syntax for the update() method −

db.update({ updated field: updated information… }, stable field: information)

让我们举个例子来理解 update() 方法如何工作。对于此示例,我们将使用以下学生数据库-

Let’s take an example to understand how the update() method works. For this example, 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"
   }
]

根据给定的数据,roll_number 为“1”的学生姓名为“elen”。以下查询将把学生姓名更新为“Adam”-

As per the given data, the name of the student with the roll_number "1" is "elen". The following query will update the student name to "Adam" −

from tinydb import TinyDB, Query
student = Query()
db.update({'st_name' : 'Adam'}, student.roll_number == 1 )

它将返回更新对象的 id-

It will return the id of the updated object −

[1]

现在,您可以使用 all() 方法来查看更新的数据库-

Now, you can use the all() method to see the updated database −

db.all()

它将显示更新后的数据-

It will display the updated data −

[
   {
      "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"
   },
   {
      "roll_number":5,
      "st_name":"karan",
      "mark":275,
      "subject":"TinyDB",
      "address":"benglore"
   }
]

有时,我们需要更新数据库中所有文档的一个或多个字段。为此,我们可以直接使用 update() 方法,而不必编写查询参数。以下查询将把所有学生的地址更改为“College_Hostel”-

Sometimes, we need to update one or more fields of all the documents in a database. For this, we can use the update() mehod directly and don’t need to write the query argument. The following query will change the address of all the students to 'College_Hostel'−

db.update({'address': 'College_Hostel'})

它将返回更新对象的 id-

It will return the ids of the updated object −

[1,2,3,4,5]

再一次,您可以使用 all() 方法来查看更新的数据库。

Again, you can use the all() method to see the updated database.

db.all()

它将显示更新后的数据-

It will show the updated data −

[
   {
      "roll_number":1,
      "st_name":"Adam",
      "mark":250,
      "subject":"TinyDB",
      "address":"College_Hostel"
   },
   {
      "roll_number":2,
      "st_name":"Ram",
      "mark":[
         250,
         280
      ],
      "subject":[
         "TinyDB",
         "MySQL"
      ],
      "address":" College_Hostel "
   },
   {
      "roll_number":3,
      "st_name":"kevin",
      "mark":[
         180,
         200
      ],
      "subject":[
         "oracle",
         "sql"
      ],
      "address":" College_Hostel "
   },
   {
      "roll_number":4,
      "st_name":"lakan",
      "mark":200,
      "subject":"MySQL",
      "address":" College_Hostel "
   },
   {
      "roll_number":5,
      "st_name":"karan",
      "mark":275,
      "subject":"TinyDB",
      "address":" College_Hostel "
   }
]

观察到所有行的地址字段都有相同的数据,即“College_Hostel”。

Observe that the address fields of all the rows have the same data, i.e., 'College_Hostel'.