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,values 是我们刚刚插入的数据。

示例:插入单个项目

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

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

广告