HTML Indexeddb Insert

Last Updated:

HTML Indexeddb Insert

In the following example, we will create a table ('MyTableName') under the database ('MyDatabase') and insert some data.


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 ="MyDatabase", 1); // Create a table 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}); x.innerHTML = "Data Added Successfully"; // Close the db when the transaction is done tx.oncomplete = function(){ db.close(); }; } </script> </body> </html>

Browser Support

Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet

Share this Page

Meet the Author