TinyDB - Update Data



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.

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()

Here, Student is the name of our database.

The update() Method

Here is the syntax for the update() method −

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

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

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 )

It will return the id of the updated object −

[1]

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

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'})

It will return the ids of the updated object −

[1,2,3,4,5]

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

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

Advertisements