TinyDB – Handling Data Query


TinyDB – Handling Data Query



”;


TinyDB – Storing Multiple Data

We have already discussed how you can use the ”insert” query to store data in a database. On a similar note, you can use the ”insert_multiple” query to store multiple data items simultaneously. Here is the syntax of ”insert_multiple” query in TinyDB:


db.insert_multiple ([
   { key1 : value1, key2 : value2, ..., keyN : valueN},
   { key1 : value1, key2 : value2, ..., keyN : valueN }
])

Let”s take a couple of examples to demonstrate how the “insert_multiple” query works. We will use the same student database that we have used in all the previous chapters.

Example 1

Let”s see how we can insert two records of students in our ”student” table using the insert_multiple query −


from tinydb import TinyDB, Query
db = TinyDB(''student.json'')
db.insert_multiple([
   {
      "roll_number":6,
      "st_name":"Siya",
      "mark":240,
      "subject":"NoSQL",
      "address":"pune"
   },
   {
      "roll_number":7,
      "st_name":"Adam",
      "mark":210,
      "subject":"oracle",
      "address":"Keral"
   }
])

It will display the document IDs of the newly saved records −


[6, 7]

Let”s check whether the new records are saved in the database or not? Use the all() method, as shown below −


db.all()

It will show all the records stored in the given table −


[
   {
      "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":6,
      "st_name":"Siya",
      "mark":240,
      "subject":"NoSQL",
      "address":"pune"
   },
   {
      "roll_number":7,
      "st_name":"Adam",
      "mark":210,
      "subject":"oracle",
      "address":"Keral"
   }
]

You can see the two new records of students have been saved in the database.

Example 2

Let”s see how we can use insert_multiple with a for loop to insert multiple values simultaneously in a table. Use the following code −


db.insert_multiple({''roll_number'': 10, ''numbers'': r} for r in range(3))

It will return the document IDs of the newly saved records −


[8, 9, 10]

Again, use the all() method to verify whether the new records have been saved in the database or not?


db.all()

It will fetch all the records stored in the given student table −


[
   {
      "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":6,
      "st_name":"Siya",
      "mark":240,
      "subject":"NoSQL",
      "address":"pune"
   },
   {
      "roll_number":7,
      "st_name":"Adam",
      "mark":210,
      "subject":"oracle",
      "address":"Keral"
   },
   {
      "roll_number":10,
      "numbers":0
   },
   {
      "roll_number":10,
      "numbers":1
   },
   {
      "roll_number":10,
      "numbers":2
   }
]

Notice the last three rows. We have used the insert_multiple method with a for loop to insert three new entries.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *