<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IndexedDB &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/indexeddb/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 10 Jul 2018 10:42:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>IndexedDB &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Introduction to IndexedDB</title>
		<link>https://www.sibeeshpassion.com/introduction-to-indexeddb/</link>
					<comments>https://www.sibeeshpassion.com/introduction-to-indexeddb/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 10 Feb 2016 00:00:39 +0000</pubDate>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Storage In HTML5]]></category>
		<category><![CDATA[IndexedDB]]></category>
		<category><![CDATA[IndexedDB And Web SQL]]></category>
		<category><![CDATA[openCursor()]]></category>
		<category><![CDATA[Storage in HTML5]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11167</guid>

					<description><![CDATA[[toc] Introduction In this post, we will see some information about IndexedDB. As the name implies, IndexedDB is a database in which we can do all kind of operations we do in a normal database. This IndexedDB has been introduced with the HTML5 . This allows a user to store a large amount of data in user&#8217;s browser. It has been proved that IndexedDB is more powerful and efficient than other storage mechanisms like local storage and session storage. Basically, IndexedDB is an API which helps the developers to do some database operations in client side, like creating a database, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>
In this post, we will see some information about IndexedDB. As the name implies, IndexedDB is a <a href="http://sibeeshpassion.com/category/database/" target="_blank">database </a>in which we can do all kind of operations we do in a normal database. This IndexedDB has been introduced with the <a href="http://sibeeshpassion.com/category/html5/" target="_blank">HTML5 </a>. This allows a user to store a large amount of data in user&#8217;s browser. It has been proved that IndexedDB is more powerful and efficient than other storage mechanisms like local storage and session storage. Basically, IndexedDB is an API which helps the developers to do some database operations in client side, like creating a database, open the transaction, creating tables, inserting values to tables, deleting values, reading the data. If you need any other way to save some data in client side, you can use <a href="http://sibeeshpassion.com/tag/storage-in-html5/" target="_blank">storage mechanisms </a> introduced in HTML5. Now we will look some of the operations a developer can do with IndexedDB. I hope you will like this.
</p>
<h3>Background</h3>
<p>
Before introducing IndexedDB, developers were familiar with Web SQL. If you need to know more about Web SQL you can see here: <a href="http://sibeeshpassion.com/introduction-to-web-sql/" target="_blank">Web SQL Introduction</a>.
</p>
<h3>Why to use IndexedDB instead Web SQL?</h3>
<p>
W3C has been announced that use of Web SQL is obsolete and deprecated, hence it is not recommended using Web SQL in your applications. Most of the modern web browsers like Mozilla does not support the use of Web SQL, this is also a great limitation of Web SQL. </p>
<p>Now we have an alternative to Web SQL, IndexedDB which more efficient and faster than Web SQL. Below are some of the main advantages of IndexedDB.</p>
<li>It stores the data as Key-Pair values</li>
<li>It is Asynchronous</li>
<li>It is non relational</li>
<li>Can access the data from the same domain</li>
<p><h3>Using the code</h3>
<p>
As you all know, the first step to work with a database is just creating it. </p>
<h4>Create/Open IndexedDB Database</h4>
<p>It is always better to check whether your browser is supporting IndexedDB or not. To check that you can use the following syntax. </p>
<p>[js]<br />
&lt;script type=&quot;text/javascript&quot;&gt;<br />
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;<br />
        //check whether the database is created or not.<br />
        if (!window.indexedDB) {<br />
            alert(&quot;Oops, Does not support IndexedDB&quot;);<br />
        }<br />
    &lt;/script&gt;<br />
[/js]</p>
<p>So if your browser is supporting IndexedDB, you are ready to go!. Cool!. Now we can create a database.  </p>
<p>[js]<br />
//check whether the database is created or not.<br />
        if (!window.indexedDB) {<br />
            alert(&quot;Oops, Does not support IndexedDB&quot;);<br />
        } else {<br />
            //Create database<br />
            var dbName = &#8216;myDB&#8217;;// Database name<br />
            var dbVersion = 2;// Database version<br />
            var crDB = window.indexedDB.open(dbName, dbVersion);<br />
        }<br />
[/js]</p>
<p>Here the first parameter is the database name and the second one is the version of your database. A version represents your database&#8217;s current schema. If everything goes well, you can see your database in your browser&#8217;s resources as follows.</p>
<div id="attachment_11168" style="width: 614px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Creation.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11168" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Creation.png" alt="IndexedDB Creation" width="604" height="255" class="size-full wp-image-11168" srcset="/wp-content/uploads/2016/02/IndexedDB-Creation.png 604w, /wp-content/uploads/2016/02/IndexedDB-Creation-300x127.png 300w, /wp-content/uploads/2016/02/IndexedDB-Creation-400x169.png 400w" sizes="(max-width: 604px) 100vw, 604px" /></a><p id="caption-attachment-11168" class="wp-caption-text">IndexedDB Creation</p></div>
<p>When you create a database there some certain events are getting fired, they are</p>
<h5>onupgradeneeded</h5>
<p>[js]<br />
crDB.onupgradeneeded = function (event) {<br />
                alert(&quot;That&#8217;s cool, we are upgrading&quot;);<br />
                db = event.target.result;<br />
                var objectStore = db.createObjectStore(&quot;UserName&quot;, { keyPath: &quot;UserID&quot; });<br />
            };<br />
[/js]</p>
<p>This event gets fired when you try upgrading your database. Here we are creating an object store with name &#8216;UserName&#8217; and index key &#8216;UserID&#8217;. Below is the output you get when the onupgradeneeded is fired. </p>
<div id="attachment_11171" style="width: 455px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert.png"><img decoding="async" aria-describedby="caption-attachment-11171" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert.png" alt="IndexedDB onupgradeneeded Alert" width="445" height="179" class="size-full wp-image-11171" srcset="/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert.png 445w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert-300x121.png 300w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert-400x161.png 400w" sizes="(max-width: 445px) 100vw, 445px" /></a><p id="caption-attachment-11171" class="wp-caption-text">IndexedDB onupgradeneeded Alert</p></div>
<div id="attachment_11172" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218.png"><img decoding="async" aria-describedby="caption-attachment-11172" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218.png" alt="IndexedDB onupgradeneeded Output" width="650" height="202" class="size-full wp-image-11172" srcset="/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218.png 650w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218-300x93.png 300w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218-400x124.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11172" class="wp-caption-text">IndexedDB onupgradeneeded Output</p></div>
<h5>onsuccess</h5>
<p>[js]<br />
crDB.onsuccess = function (event) {<br />
                alert(&quot;Awesome, You have successfully Opened a Databse!&quot;);<br />
                db = event.target.result;<br />
            }<br />
[/js]</p>
<p>If there are no errors, you will get an alert as follows.</p>
<div id="attachment_11170" style="width: 458px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert.png"><img decoding="async" aria-describedby="caption-attachment-11170" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert.png" alt="IndexedDB OnSuccess Alert" width="448" height="181" class="size-full wp-image-11170" srcset="/wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert.png 448w, /wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert-300x121.png 300w, /wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert-400x162.png 400w" sizes="(max-width: 448px) 100vw, 448px" /></a><p id="caption-attachment-11170" class="wp-caption-text">IndexedDB OnSuccess Alert</p></div>
<h5>onerror</h5>
<p>[js]<br />
 crDB.onerror = function (event) {<br />
                alert(&quot;Oops, There is error!&quot;, event);<br />
            }<br />
[/js]</p>
<p>Here crDB is our database instance. Once this is done, we can start using the transaction as we use in SQL. </p>
<h4>Creating transaction</h4>
<p>To create a transaction we can use the following syntax. We can use <em>transaction</em> method from our database instance.</p>
<p>[js]<br />
//Creating transaction<br />
            var myTran = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;);</p>
<p>            myTran.oncomplete = function (e) {<br />
                alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
            };</p>
<p>            myTran.onerror = function (e) {<br />
                alert(&quot;Oops, There is error!&quot;, e.message);<br />
            };<br />
[/js]</p>
<p>Here db is our database instance. And myTran is our transaction object which we are going to use for our upcoming operations. We are creating a transaction with read-write permission, that is why we have given a property as &#8216;readwrite&#8217; while creating a transaction. Why we use transaction is, as you all know transaction can be rollbacked. For example, if any of the operation throws an error, the transaction will be rollbacked so there won&#8217;t be any kind of mismatching data happening. And of course, we can easily manage error logs with the help of transaction. Shall we write queries needed for our operations?</p>
<h4>Adding the data</h4>
<p>Once our transaction is ready we can insert some data into our object in the transaction on success event, which we have called inside of database onsuccess event. Please check the below code.</p>
<p>[js]<br />
crDB.onsuccess = function (event) {<br />
                alert(&quot;Awesome, You have successfully Opened a Databse!&quot;);<br />
                db = event.target.result;</p>
<p>                //Creating transaction<br />
                var myTran = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;);</p>
<p>                myTran.oncomplete = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                };</p>
<p>                myTran.onsuccess = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                    //Adding the data<br />
                    var myObj = myTran.objectStore(&quot;UserName&quot;);</p>
<p>                    var myNames = [&quot;Sibi&quot;, &quot;Aji&quot;, &quot;Ansu&quot;, &quot;Shantu&quot;, &quot;Aghil&quot;];//Array with names</p>
<p>                    for (var i = 0; i &lt; 5; i++) {//Adding 5 objects<br />
                        myObj.add({ UserID: &#8216;SBKL0&#8217; + i, Name: myNames[i] });<br />
                    }<br />
                };</p>
<p>                myTran.onerror = function (e) {<br />
                    alert(&quot;Oops, There is error!&quot;, e.message);<br />
                };</p>
<p>            }<br />
[/js]</p>
<p>Here myNames is the array in which we have few names and we are using a loop to insert few data to the object. Now you can see that your data have been added to the object in your browser&#8217;s resources. </p>
<div id="attachment_11176" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204.png"><img decoding="async" aria-describedby="caption-attachment-11176" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204.png" alt="IndexedDB Adding Data To Objects" width="650" height="159" class="size-full wp-image-11176" srcset="/wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204.png 650w, /wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204-300x73.png 300w, /wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204-400x98.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11176" class="wp-caption-text">IndexedDB Adding Data To Objects</p></div>
<p>We have successfully added data. Right? Now we will see how we can remove the data we just added 🙂</p>
<h4>Removing the data</h4>
<p>If there is adding, there should be deleting :). You can delete an object from object store as follows.</p>
<p>[js]<br />
setTimeout(function () {<br />
                        db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).delete(&#8216;SBKL00&#8217;);<br />
                    }, 10000);<br />
[/js]</p>
<p>So you can rewrite the on success event of transaction s follows. </p>
<p>[js]<br />
myTran.onsuccess = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                    //Adding the data<br />
                    var myObj = myTran.objectStore(&quot;UserName&quot;);</p>
<p>                    var myNames = [&quot;Sibi&quot;, &quot;Aji&quot;, &quot;Ansu&quot;, &quot;Shantu&quot;, &quot;Aghil&quot;];//Array with names</p>
<p>                    for (var i = 0; i &lt; 5; i++) {//Adding 5 objects<br />
                        myObj.add({ UserID: &#8216;SBKL0&#8217; + i, Name: myNames[i] });<br />
                    }</p>
<p>                    setTimeout(function () {<br />
                        db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).delete(&#8216;SBKL00&#8217;);<br />
                    }, 10000);<br />
                };<br />
[/js]</p>
<p>It can give you an output as follows.</p>
<div id="attachment_11177" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects.png"><img decoding="async" aria-describedby="caption-attachment-11177" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-1024x242.png" alt="IndexedDB Deleting Data From Objects" width="634" height="150" class="size-large wp-image-11177" srcset="/wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-1024x242.png 1024w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-300x71.png 300w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-768x182.png 768w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-400x95.png 400w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects.png 1036w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11177" class="wp-caption-text">IndexedDB Deleting Data From Objects</p></div>
<p>Now we will see how we can access a particular data from the object store. </p>
<h4>Access the data with key</h4>
<p>To access a particular data, we will add a button and in the button click event, we will fetch the details needed as a request. </p>
<p>[html]<br />
 &lt;input id=&quot;btnGet&quot; type=&quot;button&quot; value=&quot;Get Name&quot; /&gt;<br />
[/html]</p>
<p>Click event is as follows.</p>
<p>[js]<br />
$(function () {<br />
            $(&#8216;#btnGet&#8217;).click(function () {<br />
                var get = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).get(&#8216;SBKL01&#8217;);<br />
                get.onsuccess = function (event) {<br />
                    alert(&quot;The name you have requested is : &quot; + get.result.Name);<br />
                };</p>
<p>            });<br />
        });<br />
[/js]</p>
<p>You can get the alert as follows, if your run your code. </p>
<div id="attachment_11179" style="width: 528px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key.png"><img decoding="async" aria-describedby="caption-attachment-11179" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key.png" alt="IndexedDB Get The Data With Key" width="518" height="340" class="size-full wp-image-11179" srcset="/wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key.png 518w, /wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key-300x197.png 300w, /wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key-400x263.png 400w" sizes="(max-width: 518px) 100vw, 518px" /></a><p id="caption-attachment-11179" class="wp-caption-text">IndexedDB Get The Data With Key</p></div>
<p>Now we will see how we can update data with the Get method. </p>
<h4>Updating the data</h4>
<p>To update the data we will have a button and in the button click we will fire the needed code. </p>
<p>[html]<br />
 &lt;input id=&quot;btnUpdate&quot; type=&quot;button&quot; value=&quot;Update Name&quot; /&gt;<br />
[/html]</p>
<p>And click event is as follows.</p>
<p>[js]<br />
$(&#8216;#btnUpdate&#8217;).click(function () {<br />
                var updObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                var upd=updObject.get(&#8216;SBKL03&#8217;);<br />
                upd.onsuccess = function (event) {<br />
                    upd.result.Name = &#8216;New Name&#8217;;<br />
                    updObject.put(upd.result);<br />
                };</p>
<p>            });<br />
[/js]</p>
<p>Here we are getting an element using the get method, and we are updating the value in the result, once it is done, we will assign that new result to the object store using put method.</p>
<p>It will give you an output as follows.</p>
<div id="attachment_11180" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key.png"><img decoding="async" aria-describedby="caption-attachment-11180" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-1024x247.png" alt="IndexedDB Update The Data With Key" width="634" height="153" class="size-large wp-image-11180" srcset="/wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-1024x247.png 1024w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-300x72.png 300w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-768x185.png 768w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-400x96.png 400w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key.png 1057w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11180" class="wp-caption-text">IndexedDB Update The Data With Key</p></div>
<p>Another interesting thing is, we can use cursors also as we use in SQL if we need to loop through some data. Here I will show you how we can do that. </p>
<h4>Using Cursor</h4>
<p>Create a button and write few code as  follows.</p>
<p>[html]<br />
&lt;input id=&quot;btnShow&quot; type=&quot;button&quot; value=&quot;Show the data&quot; /&gt;<br />
[/html]</p>
<p>Then we will create an HTML table.</p>
<p>[html]<br />
&lt;div id=&quot;myTab&quot;&gt;<br />
        &lt;table id=&quot;show&quot;&gt;<br />
            &lt;thead&gt;<br />
                &lt;th&gt;User ID &lt;/th&gt;<br />
                &lt;th&gt;Name&lt;/th&gt;<br />
            &lt;/thead&gt;<br />
            &lt;tbody&gt;&lt;/tbody&gt;<br />
        &lt;/table&gt;<br />
    &lt;/div&gt;<br />
[/html]</p>
<p>Now we can write the cursor as follows. </p>
<p>[js]<br />
$(&#8216;#btnShow&#8217;).click(function () {<br />
                var curObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                curObject.openCursor().onsuccess = function (event) { //Opening the cursor<br />
                    var cur = event.target.result;<br />
                    if (cur) {// Checks for the cursor<br />
                        $(&#8216;#show&#8217;).append(&#8216;&lt;tr&gt;&lt;td&gt;&#8217; + cur.key + &#8216;&lt;/td&gt;&lt;td&gt;&#8217; + cur.value.Name + &#8216;&lt;/td&gt;&lt;/tr&gt;&#8217;);<br />
                        cur.continue();<br />
                    }<br />
                };<br />
                $(&#8216;#myTab&#8217;).show();<br />
            });<br />
[/js]</p>
<p>Here we are creating a cursor using the function <em>openCursor()</em> and in the onsuccess event we are looping through the cursor. You can always give some styles to your table.</p>
<p>[css]<br />
&lt;style&gt;<br />
        table, tr, td, th {<br />
            border: 1px solid #ccc;<br />
            border-radius: 5px;<br />
            padding: 10px;<br />
            margin: 10px;<br />
        }<br />
    &lt;/style&gt;<br />
[/css]</p>
<p>That is all 🙂 Now when you click the show button, you can see the data in a table format as follows. </p>
<div id="attachment_11183" style="width: 311px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Using-Cursor.png"><img decoding="async" aria-describedby="caption-attachment-11183" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Using-Cursor.png" alt="IndexedDB Using Cursor" width="301" height="296" class="size-full wp-image-11183" srcset="/wp-content/uploads/2016/02/IndexedDB-Using-Cursor.png 301w, /wp-content/uploads/2016/02/IndexedDB-Using-Cursor-300x295.png 300w" sizes="(max-width: 301px) 100vw, 301px" /></a><p id="caption-attachment-11183" class="wp-caption-text">IndexedDB Using Cursor</p></div>
<h3>Complete code</h3>
<p>
[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Introduction to IndexedDB&lt;/title&gt;<br />
    &lt;script src=&quot;Scripts/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;<br />
    &lt;script type=&quot;text/javascript&quot;&gt;<br />
        var dbName = &#8216;myDB&#8217;;// Database name<br />
        var dbVersion = 2;// Database version<br />
        var crDB;<br />
        var db;<br />
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;<br />
        //check whether the database is created or not.<br />
        if (!window.indexedDB) {<br />
            alert(&quot;Oops, Does not support IndexedDB&quot;);<br />
        } else {<br />
            //Create database<br />
            crDB = window.indexedDB.open(dbName, dbVersion);<br />
            var db;<br />
            crDB.onerror = function (event) {<br />
                alert(&quot;Oops, There is error!&quot;, event);<br />
            }<br />
            crDB.onupgradeneeded = function (event) {<br />
                alert(&quot;That&#8217;s cool, we are upgrading&quot;);<br />
                db = event.target.result;<br />
                var objectStore = db.createObjectStore(&quot;UserName&quot;, { keyPath: &quot;UserID&quot; });<br />
            };<br />
            crDB.onsuccess = function (event) {<br />
                alert(&quot;Awesome, You have successfully Opened a Databse!&quot;);<br />
                db = event.target.result;</p>
<p>                //Creating transaction<br />
                var myTran = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;);</p>
<p>                myTran.oncomplete = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                };</p>
<p>                myTran.onsuccess = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                    //Adding the data<br />
                    var myObj = myTran.objectStore(&quot;UserName&quot;);</p>
<p>                    var myNames = [&quot;Sibi&quot;, &quot;Aji&quot;, &quot;Ansu&quot;, &quot;Shantu&quot;, &quot;Aghil&quot;];//Array with names</p>
<p>                    for (var i = 0; i &lt; 5; i++) {//Adding 5 objects<br />
                        myObj.add({ UserID: &#8216;SBKL0&#8217; + i, Name: myNames[i] });<br />
                    }</p>
<p>                    setTimeout(function () {<br />
                        db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).delete(&#8216;SBKL00&#8217;);<br />
                    }, 10000);<br />
                };</p>
<p>                myTran.onerror = function (e) {<br />
                    alert(&quot;Oops, There is error!&quot;, e.message);<br />
                };</p>
<p>            }<br />
        }<br />
        $(function () {<br />
            $(&#8216;#myTab&#8217;).hide();<br />
            $(&#8216;#btnGet&#8217;).click(function () {<br />
                var get = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).get(&#8216;SBKL01&#8217;);<br />
                get.onsuccess = function (event) {<br />
                    alert(&quot;The name you have requested is : &quot; + get.result.Name);<br />
                };</p>
<p>            });<br />
            $(&#8216;#btnUpdate&#8217;).click(function () {<br />
                var updObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                var upd = updObject.get(&#8216;SBKL03&#8217;);<br />
                upd.onsuccess = function (event) {<br />
                    upd.result.Name = &#8216;New Name&#8217;;<br />
                    updObject.put(upd.result);<br />
                };</p>
<p>            });<br />
            $(&#8216;#btnShow&#8217;).click(function () {<br />
                var curObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                curObject.openCursor().onsuccess = function (event) { //Opening the cursor<br />
                    var cur = event.target.result;<br />
                    if (cur) {// Checks for the cursor<br />
                        $(&#8216;#show&#8217;).append(&#8216;&lt;tr&gt;&lt;td&gt;&#8217; + cur.key + &#8216;&lt;/td&gt;&lt;td&gt;&#8217; + cur.value.Name + &#8216;&lt;/td&gt;&lt;/tr&gt;&#8217;);<br />
                        cur.continue();<br />
                    }<br />
                };<br />
                $(&#8216;#myTab&#8217;).show();<br />
            });<br />
        });<br />
    &lt;/script&gt;<br />
    &lt;style&gt;<br />
        table, tr, td, th {<br />
            border: 1px solid #ccc;<br />
            border-radius: 5px;<br />
            padding: 10px;<br />
            margin: 10px;<br />
        }<br />
    &lt;/style&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;input id=&quot;btnGet&quot; type=&quot;button&quot; value=&quot;Get Name&quot; /&gt;<br />
    &lt;input id=&quot;btnUpdate&quot; type=&quot;button&quot; value=&quot;Update Name&quot; /&gt;<br />
    &lt;input id=&quot;btnShow&quot; type=&quot;button&quot; value=&quot;Show the data&quot; /&gt;<br />
    &lt;div id=&quot;myTab&quot;&gt;<br />
        &lt;table id=&quot;show&quot;&gt;<br />
            &lt;thead&gt;<br />
                &lt;th&gt;User ID &lt;/th&gt;<br />
                &lt;th&gt;Name&lt;/th&gt;<br />
            &lt;/thead&gt;<br />
            &lt;tbody&gt;&lt;/tbody&gt;<br />
        &lt;/table&gt;<br />
    &lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>That is all. We did it. Have a happy coding.
</p>
<h3>Conclusion</h3>
<p>
Did I miss anything that you may think which is needed? Did you try Web SQL yet? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
</p>
<h3>Your turn. What do you think?</h3>
<p>
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.
</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/introduction-to-indexeddb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
