Tinydb 简明教程

TinyDB - Insert Data

我们已经创建了 TinyDB 的实例,并向其传递了一个 JSON 文件,我们的数据将存储在其中。现在是时候向我们的数据库中插入条目了。数据应采用 Python 字典的形式。

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

要插入条目,您可以使用下面语法所示的方法 insert()

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

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

我们还可以首先创建一个字典,然后使用 insert() 方法将数据插入到数据库中。

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)

在运行以上命令后,insert() 方法将返回新创建的对象的 ID。此外,我们的 JSON 文件将如下所示:

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

查看以上表中的条目:' default ' 是表的名称,' 1 ' 是新创建的对象的 ID,' values ' 是我们刚刚插入的数据。

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

它将返回新创建的对象的 ID:

It will return the ID of the newly created object −

6

让我们输入 one more record

Let’s enter one more record

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

它将返回新创建的对象的 ID:

It will return the ID of the newly created object −

7

如果您想要检查数据库中存储的条目,请按如下方式使用 all() 方法:

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

db.all()

它将生成以下 output

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

您可以看到,它在 JSON 文件中添加了两个新数据条目。

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

Example: Inserting Multiple items at a Time

您还可以在 TinyDB 数据库中一次插入多个条目。为此,您需要使用 insert_multiple() 方法。我们来看一个示例:

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)

现在,使用 all() 方法按如下方式检查数据库中存储的条目:

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

db.all()

它将生成以下 output

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

您可以观察到,它在 JSON 文件中添加了两个新的数据项。您还可以在添加最后两项时跳过数据项中的一些键值(如我们所做的那样)。我们已经跳过“mark”和“address”。

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'.