Indexeddb 简明教程

IndexedDB - Creating Data

在创建数据之前,我们需要了解如何传输数据。IndexedDB 打开事务,其每项数据操作都在这些事务中执行。每项操作有四个步骤 −

Before creating data we need to know how data is transferred. IndexedDB opens transactions and each of its data operations are carried out inside each of these transactions. Each operation has four steps −

  1. Get database object

  2. Open transaction on the database

  3. Open object store on the transaction

  4. Operate on the object store

IndexedDB 中的操作 −

Operations in IndexedDB are −

  1. create

  2. read

  3. update

  4. delete

首先,为了在数据库中执行任何操作,我们需要打开一个事务。打开事务后,我们需要获取所需的存储对象。这些对象存储仅根据创建事务时指定的存储要求提供。然后,稍后可以添加任何所需的数据。

Firstly, to perform any operation in our database, we need to open a transaction. After a transaction is opened, we need to get the object store we require. These object stores are provided only according the requirements mentioned when the transaction is created. Then whatever data that is needed can be added later.

函数用于执行给定操作(如果有)。例如,我们使用 add() 函数向数据库中添加数据或添加新条目。

Functions are used to perform the given operation (if any). For example, we use the add() function to add data into the database or to add a new entry.

Syntax

以下是向数据库中创建数据的语法 −

Following is the syntax of creating data into a database −

ar request = objectStore.add(data);

我们可以使用 add()put() 函数向对象存储中添加数据。

We can add data to an object store by using the add() or put() function.

Example

在以下示例中,我们使用 JavaScript 中的 add() 方法向对象存储中插入数据 −

In the following example, we are inserting data into the object store using the add() method in JavaScript −

<!DOCTYPE html>
<html lang="en">
<head>
   <title>creating data</title>
</head>
<body>
   <script>
      const dbName = "Database";
      var request = indexedDB.open("Database", 2);
      request.onupgradeneeded = event => {
         var db = event.target.result;
         var objectStore = db.createObjectStore("student",{ keyPath :"rollno" } );
      };
      request.onsuccess = event => {
         document.write("Database opened successfully");
         var db = event.target.result;
         var transaction = db.transaction("student", "readwrite");
         var objectStore = transaction.objectStore("student");
         objectStore.add({ rollno: 160218737028, name: "jason", branch: "IT" });
         objectStore.add({ rollno: 160218733028, name: "tarun", branch: "EEE" });
         objectStore.add({ rollno: 160218732028, name: "lokesh", branch: "CSE" });
         objectStore.add({ rollno: 160218737025, name: "abdul", branch: "IT" });
         objectStore.add({ rollno: 160218736055, name: "palli", branch: "MECH" });
      }
      transaction.oncomplete = function () {
         db.close();
      };
   </script>
</body>
</html>

Output

0 160218732028
{rollno: 160218732028, name: 'lokesh', branch: 'CSE'}
1 160218733028
{rollno: 160218733028, name: 'tarun', branch: 'EEE'}
2 160218736055
{rollno: 160218736055, name: 'palli', branch: 'CSE'}
3 160218737025
{rollno: 160218737025, name: 'abdul', branch: 'IT'}
4 160218737028
{rollno: 160218737028, name: 'jason', branch: 'IT'}