HTML Indexeddb Retrieve

Last Updated:

HTML Indexeddb Retrieve

In the following example, we will retrieve the specific data from the table ('MyTableName') under the database ('MyDatabase').

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point"></p> <script> var x = document.getElementById("point"); var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; var open = indexedDB.open("MyDatabase", 1);open.onupgradeneeded = function(){ var db = open.result; var store = db.createObjectStore("MyTableName", {keyPath: "id"}); var index = store.createIndex("NameIndex", ["name.last", "name.first"]); }; open.onsuccess = function() { // Start a new transaction var db = open.result; var tx = db.transaction("MyTableName", "readwrite"); var store = tx.objectStore("MyTableName"); var index = store.index("NameIndex"); // Add some data store.put({id: 0001, name: {first: "Brenden", last: "Eich"}, age: 58}); store.put({id: 0002, name: {first: "Dennis", last: "Ritchie"}, age: 70}); // Query the data var getEich = store.get(0001); var getRitchie = index.get(["Ritchie", "Dennis"]); getEich.onsuccess = function() { x.innerHTML += getEich.result.name.first +"<br>"; }; getRitchie.onsuccess = function() { x.innerHTML += getRitchie.result.name.first; // => "Bob" }; // Close the db when the transaction is done tx.oncomplete = function(){ db.close(); }; } </script> </body> </html>

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
indexeddbYesYesYesYesYesYesYesYesYesYesYesYesYes

Retrieve All Data

In the following example, we will retrieve all the data from the table ('MyTableName') under the database ('MyDatabase').

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point"></p> <script> var x = document.getElementById("point"); var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; var open = indexedDB.open("MyDatabase", 1); open.onupgradeneeded = function() { var db = open.result; var store = db.createObjectStore("MyTableName", {keyPath: "id"}); var index = store.createIndex("NameIndex", ["name.last", "name.first"]); }; open.onsuccess = function(){ // Start a new transaction var db = open.result; var tx = db.transaction("MyTableName", "readwrite"); var store = tx.objectStore("MyTableName"); var index = store.index("NameIndex"); // Add some data store.put({id: 0001, name: {first: "Brenden", last: "Eich"}, age: 58}); store.put({id: 0002, name: {first: "Dennis", last: "Ritchie"}, age: 70}); // Query All data var getAll = store.getAll(); getAll.onsuccess = function(){ for(i=0; i<getAll.result.length; i++) x.innerHTML += getAll.result[i].id +". "+ getAll.result[i].name.first +" <br>"; } // Close the db when the transaction is done tx.oncomplete = function(){ db.close(); }; } </script> </body> </html>

Share this Page

Meet the Author