TinyDB - 插入数据


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

句法

要插入项目,您可以使用insert()方法,其语法如下 -

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

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

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

运行上述命令后,insert()方法将返回新创建的对象的ID。而且,我们的 JSON 文件将如下所示 -

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

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

示例:插入单个项目

让我们借助示例来理解上述概念。假设我们有一个数据库,其中包含显示学号、姓名、分数、科目和地址的学生信息。以下是存储在数据库中的信息 -

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

在上面的数据库中,如果要插入新的学生记录(即单个项目),请使用以下命令 -

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

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

6

让我们再输入一项记录-

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

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

7

如果您想检查数据库中存储的项目,请使用all()方法,如下所示 -

db.all()

它将产生以下输出-

[
   {
      "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 文件中添加了两个新数据项。

示例:一次插入多个项目

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

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

现在,使用 all() 方法检查数据库中存储的项目,如下所示 -

db.all()

它将产生以下输出-

[
   {
      "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 文件中添加了两个新数据项。您还可以在添加最后两项时跳过数据项中的一些键值(就像我们所做的那样)。我们跳过了“标记”和“地址”。