Tinydb 简明教程

TinyDB - Update Data

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

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

from tinydb import Query
Student = Query()

这里 Student 是我们数据库的名称。

The update() Method

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

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

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

[
   {
      "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”-

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

它将返回更新对象的 id-

[1]

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

db.all()

它将显示更新后的数据-

[
   {
      "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”-

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

它将返回更新对象的 id-

[1,2,3,4,5]

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

db.all()

它将显示更新后的数据-

[
   {
      "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”。