Khóa học miễn phí TinyDB – Insert Data nhận dự án làm có lương

TinyDB – Insert Data



We have created the instance of the TinyDB and passed a JSON file to it where our data will be stored. It is now time to insert the items in our database. The data should be in the form of a Python dictionary.

Syntax

To insert an item, you can use insert() method whose syntax is given below −

db.insert({''type1'': ''value1'', ''type2'': ''value2'', ''typeN'': ''valueN''})

We can also create a dictionary first and then use the insert() method to insert the data into our database.

data_item = {''type1'': ''value1'', ''type2'': ''value2'', ''typeN'': ''valueN'' } db.insert(data_item)

After running the above command, the insert() method will return the ID of the newly created object. And, our JSON file will look like the one shown below −

{"_default": {"1": {"type1": "value1", "type2": "value2", "typeN": "valueN"}}}

Look at the above table entries: ”default” is the name of the table, ”1” is the ID of the newly created object, and the values are the data we have just inserted.

Example: Inserting a Single Item

Let”s understand the above concept with the help of examples. Suppose we have a
database having student information showing roll numbers, names, marks, subjects, and
addresses. Following is the information stored in the 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":"oracle",
      "address":"benglore"
   }
]

In the above database, if you want to insert a new student record (i.e., a single item), use the following command −

db.insert({
   ''roll_number'': 6,
   ''st_name'':''jim'',
   ''mark'':300,
   ''subject'':''sql'',
   ''address'':''pune''
})

It will return the ID of the newly created object −

6

Let”s enter one more record

db.insert({
   ''roll_number'': 7,
   ''st_name'':''karan'',
   ''mark'':290,
   ''subject'':''NoSQL'',
   ''address'':''chennai''
})

It will return the ID of the newly created object −

7

If you want to check the stored items in the database, use the all() method as follows −

db.all()

It will produce the following output

[
   {
      "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":"oracle",
      "address":"benglore"
   },
   {
      "roll_number":6,
      "st_name":"jim",
      "mark":300,
      "subject":"sql",
      "address":"pune"
   },
   {
      "roll_number":7,
      "st_name":"karan",
      "mark":290,
      "subject":"NoSQL",
      "address":"chennai"
   }
]

You can observe that it added two new data items in the JSON file.

Example: Inserting Multiple items at a Time

You can also insert multiple items at a time in a TinyDB database. For this, you need to use the insert_multiple() method. Let” see an example −

items = [
   {''roll_number'': 8, ''st_name'': ''petter'', ''address'': ''mumbai''},
   {''roll_number'': 9, ''st_name'': ''sadhana'', ''subject'': ''SQL''}
]
db.insert_multiple(items)

Now, check the stored items in database, using the all() method as follows −

db.all()

It will produce the following output

[
   {
      "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":"oracle",
      "address":"benglore"
   },
   {
      "roll_number":6,
      "st_name":"jim",
      "mark":300,
      "subject":"sql",
      "address":"pune"
   },
   {
      "roll_number":7,
      "st_name":"karan",
      "mark":290,
      "subject":"NoSQL",
      "address":"chennai"
   },
   {
      "roll_number":8,
      "st_name":"petter",
      "address":"mumbai"
   },
   {
      "roll_number":9,
      "st_name":"sadhana",
      "subject":"SQL"
   }
]

You can observe that it added two new data items in the JSON file. You can also skip some key values from the dataitems (as we have done) while adding the last two items. We have skipped ”mark” and ”address”.

Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc