TinyDB - 更新数据


TinyDB可以存储多种格式的数据,我们可以使用各种方法轻松检索存储的数据。但有时,我们需要更新数据,为此我们可以使用update()方法。

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

from tinydb import Query
Student = Query()

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

update() 方法

这是 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”。