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

根据给定的数据,学号为“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”。

广告