Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ตัวอย่าง HTML5 IndexedDB


ฟังก์ชันต่อไปนี้เป็นตัวอย่าง IndexedDB สำหรับเพิ่มข้อมูล:

function add() {
  var request = db.transaction(["employee"], "readwrite")
    .objectStore("employee")
    .add({ id: "001", name: "Amit", age: 28, email: "demo1@example.com" });
  request.onsuccess = function(event) {
    alert("Amit has been added to your database.");
  };
  request.onerror = function(event) {
    alert("Unable to add data\r\nAmit is already exist in your database!");
  }
}

ข้างต้นเราเพิ่มรายละเอียดต่อไปนี้ในฐานข้อมูล:

const employeeData = [
  { id: "001", name: "Amit", age: 28, email: "demo1@example.com" },
];