Back to Top

How to use IndexedDB in HTML5

html5 indexed db

IndexedDB in HTML5 is introduced for storing large amounts of structured data on the client side in WebBrowser.It is also known as The Indexed Database API in HTML5.

HTML5 provides the local database storage which is known as IndexedDB using API. IndexedDB in HTML5 is a more powerful option for storing large amounts of structured data.To store data in local system improves the Web application accessibility and responsiveness of applications.

[sociallocker]

IndexedDB API is used to create a rich data-intensive database which store locally for HTML5 web applications.It also helps to cache the data for online web or mobile applications to run and respond faster.

In this article, We will see how to manage IndexedDB for web application in HTML5. HTML5 IndexedDB is the replacement of cookie because it is more reliable data storage for large applications.

Also, To store a data locally makes application more faster, uses a less bandwidth and meet efficiency.HTML5 IndexedDB improves the richness of the browser because of WebStorage and is also called a lightweight NOSQL database.

LocalStorage

:

The first option is localStorage which allows you to use a simple key-value pair to store data locally. IndexedDB supports the local storage using large objects and retrieves data using robust data access mechanism.

IndexedDB API replaces the Web Storage API so it is not recommended for use in the HTML5 specification.Some of the leading browsers still support Web Storage, including Apple’s Safari and Opera Web browser. With compared to Web Storage, IndexedDB has several advantages, including indexing, transaction processing, and robust. In this post, we will go to learn IndexedDB with examples and will also show how to manage IndexedDB database.

Basics about Indexed DB

Indexed DB does not have a rigid table structure like MySQL which contain fixed database tables, but it is store information as collections of records like MONGODB.You can create one or more IndexedDB databases for the site with a unique name.

A database may contain one or more Object Stores. An object is saved by the unique name and a collection of records. Each record has a key and a value pair.Values ​​can be anything like strings, numbers, objects, arrays, blobs etc which can have one or more attributes.A Key can be a string, a number or an array be and identifies a record. It may be a single identifier or multiple JavaScript separated by periods identifier.IndexedDB API supports an asynchronous and a synchronous API.

Let’s start to understand Indexed DB API with example and step by step:

1. Browser Support:

The first step is to check whether browser support IndexedDB or not. So Let’s check by using below code:

2.Create Database

Each database using Indexed DB can have only one version number. When you create a first database, it’s initial version number is zero. After creating the database, version number is used for internal versioning of web application. After creating a database, If you want to change database structure, the upgradeneeded event is used.When you trigger upgradeneeded event handler, you must open the database first.

Remember: Make sure the database or Object Stores must be located onupgradeneeded event handler when you want to modify it.

The following code snippet shows how to create a database.First of all, call an open method and pass the database name. If a database with the specified name does not exist, it will create the database.

IndexedDB is recommended for creating tables during initialization. After open each site in the browser, first check the version number. You do not need to create the database of the site a second time. The table in indexedDB is called object store.

When creating database, one of the following events fire on the request:

  1. error:if something goes wrong when you create the database
  2. upgradeneeded: when the database version has changed or create the database first.
  3. Success: when the already created database opened.It might be fire after upgradeneeded Event.

3. Delete the existing database

To delete an existing database, you can call deleteDatabase method and in that pass the name of the database you want to delete, as per following code snippet:

Create and delete the database is very easy to use So this is the very basics for the Indexed DB in HTML5. Next we will learn how to use object stores in Indexed DB.

If you don’t want to miss any upcoming article in this series then subscribe our RSS Feed via mail, Like us on Facebook or Follow us no Twitter. If you do so, I am sure you will never miss any article from us.
[/sociallocker]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Inheritance in object-oriented PHP

Posted on 7 years ago

Bhumi