ฟังก์ชันต่อไปนี้เป็นตัวอย่างของ 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" },
];