Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • IndexedDB

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 324
    Comment on it

    The indexeddb is used to store the data in the browser. It is not a relational database and stores values in the form of key-pair.

    There are different methods used for performing different actions for a database like add(), get(), remove().

    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
    
    window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
    window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange
    
    if (!window.indexedDB) {
       window.alert("Your browser doesn't support a stable version of IndexedDB.")
    }
    

    Create database:

    const studentInfo = [
       { id: "01", name: "Swati", age: 21, email: "swati@gmail.com" },
       { id: "02", name: "Pooja", age: 20, email: "pooja@gmail.com" }
    ];
    

    Adding data to database:

    function add() {
       var request = db.transaction(["student"], "readwrite")
       .objectStore("student")
       .add({ id: "02", name: "Pooja", age: 20, email: "pooja@gmail.com" });
    
       request.onsuccess = function(event) {
          alert("added to your database.");
       };
    
       request.onerror = function(event) {
          alert("Unable to add data or already exist in your database! ");
       }
    }
    

    Retrieve Data:

    function read() {
       var transaction = db.transaction(["student"]);
       var objectStore = transaction.objectStore("student");
       var request = objectStore.get("00-03");
    
       request.onsuccess = function(event) {
          if(request.result) {
             alert("Name: " + request.result.name + ", Age: " + request.result.age + ", Email: " + request.result.email);
          }
    
          else {
             alert("not found database!");  
          }
       };
    
       request.onerror = function(event) {
          alert("Unable to retrieve data!");
       };
    
    }
    

    Remove Data:

    function remove() {
       var request = db.transaction(["student"], "readwrite")
       .objectStore("student")
       .delete("02");
    
       request.onsuccess = function(event) {
          alert("Removed from database.");
       };
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: