IndexedDB – Deleting Data

IndexedDB – Deleting Data ”; Previous Next There are many situations where we need to delete data from the database; be it for storage purposes or just removing unwanted data to free up space. If we want to delete this unnecessary data from a database we can use the .delete() function Syntax const request = objectStore.delete(data); We use the delete() function to delete the fields of the database which are not required. Example Let us look at an example script to delete data − <!DOCTYPE html> <html lang=”en”> <head> <title>Document</title> </head> <body> <script> const request = indexedDB.open(“botdatabase”,1); request.onupgradeneeded = function(){ const db = request.result; const store = db.createObjectStore(“bots”,{ keyPath: “id”}); } request.onsuccess = function(){ document.write(“database opened successfully”); const db = request.result; const transaction=db.transaction(“bots”,”readwrite”); const store = transaction.objectStore(“bots”); store.add({id: 1, name: “jason”,branch: “IT”}); store.add({id: 2, name: “praneeth”,branch: “CSE”}); store.add({id: 3, name: “palli”,branch: “EEE”}); store.add({id: 4, name: “abdul”,branch: “IT”}); store.put({id: 4, name: “deevana”,branch: “CSE const deletename = store.delete(1); deletename.onsuccess = function(){ document.write(“id : 1 has been deleted”); } transaction.oncomplete = function(){ db.close; } } </script> </body> </html> Output database opened successfully id : 1 has been deleted The database after deletion of id:1 = 0 2 {id: 2, name: ”praneeth”, branch: ”CSE”} 1 3 {id: 3, name: ”palli”, branch: ”EEE”} 2 4 {id: 4, name: ”deevana”, branch: ”CSE”} Print Page Previous Next Advertisements ”;

IndexedDB – Home

IndexedDB Tutorial PDF Version Quick Guide Resources Job Search Discussion Indexed Database is a type of NoSQL database or Non-relational structured query language. It is a transactional database system like an SQL-based RDBMS. However, unlike SQL-based RDBMSs, which use fixed-column tables, IndexedDB is a JavaScript-based object-oriented database. Audience This tutorial is designed for Software Professionals who are willing to learn IndexedDB Database in simple and easy steps. It will throw light on IndexedDB concepts and after completing this tutorial you will be at an intermediate level of expertise, from where you can take yourself at higher level of expertise. Prerequisites Before you start proceeding with this tutorial, we are assuming that you have a brief knowledge understanding of JavaScript. Print Page Previous Next Advertisements ”;

IndexedDB – Introduction

IndexedDB – Introduction ”; Previous Next A database management system provides a mechanism for the storage and retrieval of data. There are various kinds of databases available mostly used ones among them are − Hierarchical databases Network databases Object-oriented databases Relational databases NoSQL databases NoSQL databases A NoSQL database (sometimes called Not Only SQL) is a database that provides a mechanism to store and retrieve data other than the tabular relations used in relational databases. These databases are schema-free, support easy replication, have simple API, are eventually consistent, and can handle huge amounts of data (big data). There are different types of NoSQL databases also like − Document databases. Key-value stores. Column-oriented databases. Graph databases. What is IndexedDB Indexed Database is a type of NoSQL database or Non−relational structured query language. It is a transactional database system, like an SQL−based RDBMS. However, unlike SQL−based RDBMSs, which use fixed-column tables, IndexedDB is a JavaScript−based object-oriented database. It is used when we need to store significant amounts of data on the server side and faster than local storage. Since it stores the data in the browser it can be used online and offline too. Using this, you can create a web application (with rich query abilities) that can run whether an internet connection is available or not. Key Characteristics of IndexedDB Following are the key characters of the IndexedDB database − IndexedDB is a NoSQL database that stores key−value pairs. It can store almost any kind of value by keys or multiple key types. As mentioned, IndexedDB follows a transactional database model − Transaction is a wrapper class around the operation or group of operations so that data integrity is maintained. You don”t want data to be altered or missed so if the transaction is failed a callback is rolled out. IndexedDB does not use Structured Query Language − Since IndexedDB uses a NoSQL database it doesn’t use SQL, instead, it uses queries on Indexes to produce data via cursors or the getAll() method to iterate around different sets. IndexedDB uses a lot of requests − Requests are objects that receive the success or failure of DOM events (DOM – HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document). DOM events are success or error which has a target property that indicates the flow of the request. Success events can’t be canceled but, error events can be canceled. There are a lot of requests in IndexedDB like on success, onerror and addEventListener(), removeEventListener() on them. For getting to know the status of the request we also have ready state, result, and error code properties. IndexedDB needs to follow the same origin − Origin is the URL of the document in which the script is being written, each origin has some databases under it and each database has its name to be identified by the origin. The security boundary imposed on IndexedDB prevents applications from accessing data with a different origin. For example, if we take a URL and take different subdirectories of it, it can retrieve data but if we change location to port 8080 and try to retrieve data from the usual URL and the changed port, we cannot retrieve the data. Terminology Following are various important terms in indexedDB that you should know before proceeding further − Database − In IndexedDB database is the highest level which contains the object stores that contain the data. Object Stores − Object stores are the data storage entities of IndexedDB. Think of it as tables in RDBMS where we store data based on the type of data we want to store (ex: id, name, roll no, etc). Transaction − For any database operation we do the following process. Get database object Open transaction on the database Open object store on the transaction and then, operate on object store. So basically, a transaction is a wrapper function that is connected to every database and it ensures data integrity such that if the transaction is canceled or any error happens it will call back to where the transaction has not yet been begun. Index − Think of object store as a table and we use indexes to retrieve data of a single property from them. Ex: Name, age, etc. Cursor − In a database, if we need to traverse multiple records from object stores we use cursors. Support for IndexedDB IndexedDB is a database in a browser so, we need to check if it is supported by the current/existing browser. To do so, paste the following code in an text editor save it as test.html and run it your browser. const indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; if (!indexedDB) { document.write(“IndexedDB could not be found in this browser.”); } const request = indexedDB.open(“MyDatabase”, 1); If IndexedDB is supported by your browser this program gets executed successfully, an a database will be created. Print Page Previous Next Advertisements ”;

IndexedDB – Creating Data

IndexedDB – Creating Data ”; Previous Next Before creating data we need to know how data is transferred. IndexedDB opens transactions and each of its data operations are carried out inside each of these transactions. Each operation has four steps − Get database object Open transaction on the database Open object store on the transaction Operate on the object store Operations in IndexedDB are − create read update delete Firstly, to perform any operation in our database, we need to open a transaction. After a transaction is opened, we need to get the object store we require. These object stores are provided only according the requirements mentioned when the transaction is created. Then whatever data that is needed can be added later. Functions are used to perform the given operation (if any). For example, we use the add() function to add data into the database or to add a new entry. Syntax Following is the syntax of creating data into a database − ar request = objectStore.add(data); We can add data to an object store by using the add() or put() function. Example In the following example, we are inserting data into the object store using the add() method in JavaScript − <!DOCTYPE html> <html lang=”en”> <head> <title>creating data</title> </head> <body> <script> const dbName = “Database”; var request = indexedDB.open(“Database”, 2); request.onupgradeneeded = event => { var db = event.target.result; var objectStore = db.createObjectStore(“student”,{ keyPath :”rollno” } ); }; request.onsuccess = event => { document.write(“Database opened successfully”); var db = event.target.result; var transaction = db.transaction(“student”, “readwrite”); var objectStore = transaction.objectStore(“student”); objectStore.add({ rollno: 160218737028, name: “jason”, branch: “IT” }); objectStore.add({ rollno: 160218733028, name: “tarun”, branch: “EEE” }); objectStore.add({ rollno: 160218732028, name: “lokesh”, branch: “CSE” }); objectStore.add({ rollno: 160218737025, name: “abdul”, branch: “IT” }); objectStore.add({ rollno: 160218736055, name: “palli”, branch: “MECH” }); } transaction.oncomplete = function () { db.close(); }; </script> </body> </html> Output 0 160218732028 {rollno: 160218732028, name: ”lokesh”, branch: ”CSE”} 1 160218733028 {rollno: 160218733028, name: ”tarun”, branch: ”EEE”} 2 160218736055 {rollno: 160218736055, name: ”palli”, branch: ”CSE”} 3 160218737025 {rollno: 160218737025, name: ”abdul”, branch: ”IT”} 4 160218737028 {rollno: 160218737028, name: ”jason”, branch: ”IT”} Print Page Previous Next Advertisements ”;

IndexedDB – Object Stores

IndexedDB – Object Stores ”; Previous Next Object stores are the data storage of IndexedDB. It is where data is stored. A database may have multiple object stores. Think of them as tables in RDBMS where we store data based on the type of data we want to store. To ensure database integrity, object stores can only be created and removed using the callback function idb.open(). It contains a method named createObjectStore() which is used to create an object-store. Creating object Stores You can create an object store using the createObjectStore() method. Following is the syntax of this method − IDBDatabase.createObjectStore(name); Or, IDBDatabase.createObjectStore(name, options); Where, The name is the name of the object store. The options object can let us define various configuration properties. Example Following example creates a new database and creates a object store in it − <!DOCTYPE html> <html lang=”en”> <head> <title>Creating Object Store</title> </head> <body> <script> var request = indexedDB.open(“myDatabase”, 2); request.onupgradeneeded = event => { var db = event.target.result; var objectStore = db.createObjectStore(“customers”); document.write(“Object store Created Successfully…”); }; </script> </body> </html> Output On executing, the above program displays the following message on the browser. Object store Created Successfully… Verification If the above program is executed successfully if you expand “myDatabase” you can see the newly created object-store. Defining primary keys Similar to RDBMS we need primary keys to uniquely define some data in an object store. It can be done in 2 ways using a key path or a key generator. Keypath and Key generator A key path is a property that always exists and contains a unique value. We can choose a unique value such as an email address for example. A key generator creates a unique value for every object added to the object store. By default, if we don”t mention a key generator comes into the picture. Ex, auto-increment. Syntax Following is the syntax ro create a keypath on an object store. var objectStore = db.createObjectStore(“ObjectStoreName”, { keyPath: “primary key, autoincrement/autoDecrement : true” }); Example In the example given below, we are creating a keypath to an object store using JavaScript − <!DOCTYPE html> <html lang=”en”> <head> <title>keypath</title> </head> <body> <script> var request = indexedDB.open(“myDtabase”, 2); request.onupgradeneeded = event => { var db = event.target.result; var objectStore = db.createObjectStore(“customers”,{keyPath:”id”, autoIncrement:true}); document.write(“Object store Created Successfully…”); }; </script> </body> </html> Output On executing the above example, it will display the following text on the browser − Object store Created Successfully… Verification If the above program is executed successfully if you expand “myDatabase” you can see the newly created object store, if you click on it you can observe that the keypath is created for “id”. When new object stores are created they are mentioned in the IndexedDB folders like above. You can use both a keypath and a key generator. If the data is always unique we can use a keypath else if the value changes you can use a key generator, if you want to change the values for every value but want to give a value that uniquely represents the store we can use both. Defining Indexes Indexes are a kind of object-store. They are used to retrieve data from the reference object, stored by a specified property. An index uses the specified property as its key path instead of the reference store”s primary key. To create an index, you need to call the createIndex() method on an object store instance. Syntax Following is the syntax of the createIndex() method − var myIDBIndex = objectStore.createIndex(indexName, keyPath); var myIDBIndex = objectStore.createIndex(indexName, keyPath, Parameters); Where, An indexName is the name of the index created. Keypath is the primary definition while creating an object store The value of the last parameter can be unique or multi-entry In case you “pass unique: true”. The index will not allow duplicate values for a single key. If you pass “multi-entry: true”. The index will add an entry for each array element when the keyPath resolves to an Array. If false, it will add one single entry containing the Array. Example The following JavaScript example demonstrates how to create indexes. <!DOCTYPE html> <html lang=”en”> <head> <title>OPENING A DATABASE</title> </head> <body> <script> const dbName = “myDB”; const studentdata = [ {name : “jason” , rollno: “160218737028” , branch : “IT”}, {name : “lokesh” , rollno: “160218735020” , branch : “CSE”}, {name : “tarun” , rollno: “160218733057” , branch : “EEE”}, {name : “pranith” , rollno: “160218737029” , branch : “IT”} ]; var request = indexedDB.open(“myDB”, 2); request.onupgradeneeded = event => { var db = event.target.result; var objectStore = db.createObjectStore(“student”,{ keyPath :”rollno” }); objectStore.createIndex(“name”, “name”, { unique: false }); objectStore.createIndex(“branch”, “branch”, { unique: false }); objectStore.transaction.oncomplete = event => { var objectStore = db.transaction(“student”, “readwrite”).objectStore(“student”); studentdata.forEach(function(student) { objectStore.add(student); }); }; }; </script> </body> </html> Output If you go and verify the contents of the IndexedDB database myDB and expand it youj can observe the created table as − If you click on the name and student values you can observe the index values as − Name index # Key(Key path:”name”) Primary key (Key path:”rollno”) Value 0 “jason” “160218737028” {name: ”jason”, rollno: ”160218737028”, branch: 1. branch: “IT” 2. name: “jason” 3. rollno: “160218737028” 1 “lokesh” “160218735020” {name: ”lokesh”, rollno: ”160218735020”, branch: ”CSE”} 1. branch: “CSE” 2. name: “lokesh” 3. rollno: “160218735020” 2 “pranith” “160218737029” {name: ”pranith”, rollno: ”160218737029”, branch: ”IT”} 1. branch: “IT”

IndexedDB – Installation

IndexedDB – Installation ”; Previous Next Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. You can download the visual studio code from its official website – https://code.visualstudio.com Select the version you want based on your PC configuration and OS. Once downloaded you can install it directly on your computer. Installing the Visual Studio Code installer on Windows First of all download the Visual Studio Code installer for Windows as specified above − Once it is downloaded, run the installer. Then, accept the agreement and click on next. Now, click on “create a desktop icon” so that it can be accessed from the desktop, and click on Next. Then, click on the install button. Finally, after installation completes, click on the finish button, and the visual studio code will open. Now Visual Studio code is successfully installed on your device, start writing code on this code editor. Downloading, Installing, and Creating a Node.js project (optional) Now after installing the visual studio code we need to install Node.js Downloading Node.JS You can download Node.js from its official website which is https://nodejs.org/en/. Select the version of your choice based on your computer”s configuration. The LTS version is preferable as it is a more stable version and it stands for Long Term Support. Installing Node.js Follow the steps given below to install Node.js in your system − Step 1 − Now that the Node.js has opened. You’ll find this window pop up Click on next. Step 2 − You will be redirected to the “End-User License Agreement” window. Accept the agreement and click on Next. Step 3 − In the next window you need to select the “Destination Folder”. Change the existing folder or, use the default folder mentioned and then click Next. Step 4 − Click Next in the “Custom Setup” and “Tools For Native Modules” windows. Step 5 − Now, thw setup is ready click Install, to install the selected modules. Print Page Previous Next Advertisements ”;

IndexedDB – Connection

IndexedDB – Connection ”; Previous Next A database is an organized collection of structured data stored in a computer system. To perform operations on data we need to connect to a database. In this chapter, we will discuss how to create/connect to a database, open a database, and delete a database. Creating a database − You can create a database in IndexedDB using the open() function. Following is the syntax of this function. let openRequest = indexedDB.open(name, version); Where, name is the name of the database you need to create. version is the version of the database that is to be created. The default value of this parameter is 1. If you omit this value, the version is considered as 1. The version value you pass to this function should not be less than the current version (of the IndexedDB). This function returns 1 if the database is created successfully and it returns 0 in case of a failure. Example Following is the example to create a database in IndexedDB <!DOCTYPE html> <html lang=”en”> <head> <title>Indexed db</title> </head> <body> <script> //Creating a database const request = indexedDB.open(“myDatabase”, 1); if(indexedDB){ document.write(“Database Created……”); } </script> </body> </html> Output If you save the above code in a file “test.html” and run it, the following message will be displayed on the browser − Database Created…… Verification Since IndexedDB is the browser”s built-in database you observe the created database in the browser itself. Right-click on the resultant page, click on inspect element and select the Application tab. You can see the IndexedDB database there if you expand it you can see the created database file as shown below − Generating Handlers An event is an action performed on an HTML element. Using JavaScript we can handle these events. From now on we use JavaScript handlers (to make this clearer). If the request is a success, we use the onsuccess event. request.onerror = event => { // Do something (ex: document.write(“error”); }; If the request is a failure, we use onerror event. request.onsuccess = event => { // Do something (ex : document.write(“success”); }; When you create a database or increase the version number of an existing database we use onupgradeneeded event. request.onupgradeneeded = event => { var db = event.target.result; }; Example Following example displays the message “database creation success”. If the database creation is successful. In here we are using the onsuccess and onerror handlers to display these messages. <!DOCTYPE html> <html lang=”en”> <head> <title>Handlers</title> </head> <body> <script> const request = indexedDB.open(“DATABASE”, 1); request.onsuccess = function (){ document.write(“Database created successfully…”) } request.onerror = function(){ document.write(“database creation error”); } request.onupgradeneeded = function(event){ var db = event.target.result; } </script> </body> </html> Output If you save the above code in a file “test.html” and run it, the following message will be displayed on the browser − Database created successfully… Connecting to an existing database To interact with IndexedDB, we use JavaScript. The code we write in JavaScript doesn”t interact with databases directly. We need to connect with the database using a connection object to manipulate the objects of the database. Opening a database directly creates a connection. There can be multiple connections to a database. When a connection is initially created it is in the open state. You can connect to an IndexedDB database using the open() function (which we used to create a database). Syntax Following is the syntax to connect to an existing database. let openRequest = indexedDB.open(name, version); Example A JavaScript example that interacts with an existing database using a connection object is given below − <!DOCTYPE html> <html lang=”en”> <head> <title>OPENING A DATABASE</title> </head> <body> <script> const request = indexedDB.open(“DATABASE”, 1); request.onsuccess = function (){ document.write(“<br> Database created successfully”) } const requestone = indexedDB.open(“Database1”,2); requestone.onsuccess = function(){ document.write(“<br> Database created successfully”); } const requesttwo = indexedDB.open(“DATABASE”,1); requesttwo.onsuccess = function(){ document.write(“<br> Database opened successfully”); } </script> </body> </html> Output The above program prints the following output on the browser − Database created successfully Database opened successfully Database created successfully If the request is a success, then the event named onsuccess will be called. Another way to check databases in the browser In addition to the inspect element, there is another way to check the IndexedDB database in the browser. In the top right corner, there will be a customize and control button, Click it. Select the More tools option in the list and then select Developer tools. On the next page select the Application tab where you can see the IndexedDB database. Deleting a database If there is an excess of any database which we do not need or it unnecessarily occupies space, we can delete it. To delete a database we can use the deleteDatabase() function. Following is the syntax of the deleteDatabase() function − let deleteRequest = indexedDB.deleteDatabase(name) Here, the name parameter is the name of the database that we want to delete. Example Following example creates a database named TestDatabase and deletes it using the deleteDatabase() function. <!DOCTYPE html> <html lang=”en”> <head> <title>Indexed db</title> </head> <body> <script> const request = indexedDB.open(“TestDatabase”, 1); request.onsuccess = function () { document.write(“Database Created Successfully”); }; var req = indexedDB.deleteDatabase(“TestDatabase”); req.onsuccess = function () { document.write(“Database Deleted Successfully”); }; req.onerror = function () { document.write(“Couldn”t delete the database”); }; </script> </body> </html> Deleting a database Directly From the browser Once you create a database, you can directly delete it database from the browser. To do so, follow the steps given below − Step 1 − Open the page

IndexedDB – Ranges

IndexedDB – Ranges ”; Previous Next When we don’t want to get all the data at once we use ranges. When we want to get data in a specific range only we use ranges. We define the range using the IDBKeyRange object. This object has 4 methods which are − upperBound() lowerBound() bound() only() Syntax IDBKeyRange.lowerBound(indexKey); IDBKeyRange.upperBound(indexKey); IDBKeyRange.bound(lowerIndexKey, upperIndexKey); Following is the list of various range codes − S.No. Range Codes & Description 1 All keys ≥ a DBKeyRange.lowerBound(a) 2 All keys > a IDBKeyRange.lowerBound(a, true) 3 All keys ≤ b IDBKeyRange.upperBound(b) 4 All keys < b IDBKeyRange.upperBound(b, true) 5 All keys ≥ a && ≤ b IDBKeyRange.bound(a, b) 6 All keys > a &&< b IDBKeyRange.bound(a, b, true, true) 7 All keys > a && ≤ b IDBKeyRange.bound(a, b, true, false) 8 All keys ≥ a && < b IDBKeyRange.bound(a, b, false, true) 9 The key = c IDBKeyRange.only(c) We generally use the indexes for using range and in the syntax, the index key represents the indexes keypath value. Examples Various examples to retrieve the range codes using get() and getAll() methods is as follows − class.get(‘student’) class.getAll(IDBKeyRange.bound(‘science’,’math’) class.getAll(IDBKeyRange.upperbound(‘science’,true) class.getAll() class.getAllKeys(IDBKeyRange.lowerbound(‘student’,true)) HTML Example Consider the HTML example below to obtain the range codes − <!DOCTYPE html> <html lang=”en”> <head> <title>Document</title> </head> <body> <script> const request = indexedDB.open(“botdatabase”,1); request.onupgradeneeded = function(){ const db = request.result; const store = db.createObjectStore(“bots”,{ keyPath: “id”}); store.createIndex(“branch_db”,[“branch”],{unique: false}); } request.onsuccess = function(){ document.write(“database opened successfully”); const db = request.result; const transaction=db.transaction(“bots”,”readwrite”); const store = transaction.objectStore(“bots”); const branchIndex = store.index(“branch_db”); store.add({id: 1, name: “jason”,branch: “IT”}); store.add({id: 2, name: “praneeth”,branch: “CSE”}); store.add({id: 3, name: “palli”,branch: “EEE”}); store.add({id: 4, name: “abdul”,branch: “IT”}); store.put({id: 4, name: “deevana”,branch: “CSE”}); const upperquery =store.getAll(IDBKeyRange.upperBound(”2”, true)); upperquery.onsuccess = function(){ document.write(“upperquery”,upperquery.result); } transaction.oncomplete = function(){ db.close; } } </script> </body> </html> Output database opened successfully upperquery (4) [{…}, {…}, {…}, {…}] arg1: (4) [{…}, {…}, {…}, {…}] 0: {id: 1, name: ”jason”, branch: ”IT”} 1: {id: 2, name: ”praneeth”, branch: ”CSE”} 2: {id: 3, name: ”palli”, branch: ”EEE”} 3: {id: 4, name: ”deevana”, branch: ”CSE”} length: 4 [[Prototype]]: Array(0) [[Prototype]]: Object Print Page Previous Next Advertisements ”;

IndexedDB – Indexes

IndexedDB – Indexes ”; Previous Next Indexes are a kind of object store used to retrieve data from the reference object stored by a specified property. Even though an index is inside the reference object store and contains the same data, instead of the reference store”s primary key it uses the specified property as its key path. Indexes are used to define a unique constraint on your data and they are made when the object stores are created. To create an index, call the createIndex method on an object store instance − Syntax var myIDBIndex = objectStore.createIndex(indexName, keyPath); var myIDBIndex = objectStore.createIndex(indexName, keyPath, objectParameters); This method creates and returns an index object. The method creates an index that takes the following arguments − Index name − The name of the index. Keypath − We mention the primary key here. Object Parameters − There are two object parameters. Unique − Duplicate values cannot be added. Multi entry − If true, the index will add an entry in the index for each array element when the keyPath resolves to an Array. If false, it will add one single entry containing the Array. Example The following example shows the implementation of indexes in an object store − <!DOCTYPE html> <html lang=”en”> <head> <title>Document</title> </head> <body> <script> const request = indexedDB.open(“botdatabase”,1); request.onupgradeneeded = function(){ const db = request.result; const store = db.createObjectStore(“bots”,{ keyPath: “id”}); store.createIndex(“branch_db”,[“branch”],{unique: false}); } request.onsuccess = function(){ document.write(“database opened successfully”); const db = request.result; const transaction=db.transaction(“bots”,”readwrite”); const store = transaction.objectStore(“bots”); const branchIndex = store.index(“branch_db”); store.add({id: 1, name: “jason”,branch: “IT”}); store.add({id: 2, name: “praneeth”,branch: “CSE”}); store.add({id: 3, name: “palli”,branch: “EEE”}); store.add({id: 4, name: “abdul”,branch: “IT”}); store.put({id: 4, name: “deevana”,branch: “CSE”}); transaction.oncomplete = function(){ db.close; } } </script> </body> </html> Output branchIndex: 1 [”CSE”] 0: “CSE” length: 1 4 {id: 4, name: ”deevana”, branch: ”CSE”} branch: “CSE” id: 4 name: “deevana” 2 [”EEE”] 0: “EEE” length: 1 3 {id: 3, name: ”palli”, branch: ”EEE”} branch: “EEE” id: 3 name: “palli” 3 [”IT”] 0: “IT” length: 1 1 {id: 1, name: ”jason”, branch: ”IT”} branch: “IT” id: 1 name: “jason” Print Page Previous Next Advertisements ”;

IndexedDB – Reading Data

IndexedDB – Reading Data ”; Previous Next We enter data into the database, and we need to call the data to view the changes and also for various other purposes. We must call the get() method on the object store to read this data. The get method takes the primary key of the object you want to retrieve from the store. Syntax var request = objectstore.get(data); Here, we are requesting the objectstore to get the data using get() function. Example Following example is an implementation of requesting the objectstore to get the data − <!DOCTYPE html> <html lang=”en”> <head> <title>Document</title> </head> <body> <script> const request = indexedDB.open(“botdatabase”,1); request.onupgradeneeded = function(){ const db = request.result; const store = db.createObjectStore(“bots”,{ keyPath: “id”}); } request.onsuccess = function(){ document.write(“database opened successfully”); const db = request.result; const transaction=db.transaction(“bots”,”readwrite”); const store = transaction.objectStore(“bots”); store.add({id: 1, name: “jason”,branch: “IT”}); store.add({id: 2, name: “praneeth”,branch: “CSE”}); store.add({id: 3, name: “palli”,branch: “EEE”}); store.add({id: 4, name: “abdul”,branch: “IT”}); const idquery = store.get(4); idquery.onsuccess = function(){ document.write(“idquery”,idquery.result); } transaction.oncomplete = function(){ db.close; } } </script> </body> </html> Output database opened successfully idquery {id: 4, name: ”abdul”, branch: ”IT”} Print Page Previous Next Advertisements ”;