Using the Web SQL Database API in HTML 5September 14, 2010 One of the HTML 5 specifications, Web SQL database, enables offline data storage by defining an API for storing structured data within the client. Yuvarani Meiyappan introduces the new Web SQL database feature in HTML5 and its API, and then dives into using Web SQL database with a demo for implementing a shopping cart. The HTML 5 specification has introduced a number of new elements, attributes and APIs to increase the quality, speed and richness of Web applications, and a few browsers including Google Chrome have already implemented many of these exciting features. One of the HTML 5 specifications, Web SQL database enables offline data storage by defining an API for storing structured data within the client. Unlike HTML 5's Web Storage API, which persists data in key-value pairs on the client, the Web SQL database persists the data in a database on the client. The Web SQL database is asynchronous and can be queried and manipulated using the API through JavaScript. In this article, I introduce the new Web SQL database feature in HTML5 and its API. We then dive into using Web SQL database with a demo for implementing a shopping cart. The Interfaces of the Web SQL Database APIIn this section, we explore some of the interfaces and methods that are available in the Web SQL database API. Database APIThe Database API of Web SQL database has the WindowDatabase and WorkerUtils interfaces. The interfaces have the Here is the syntax:
Here is a code example:
The last statement in the above code would create a database. Asynchronous Database APIThe Asynchronous database API has an interface called Database. The Database interface has the transaction and readTransaction methods, which take up to three arguments. The transaction method opens the database in read/write mode, whereas the readTransaction method opens the database in read mode. Here is the syntax:
Because the method is asynchronous, it would immediately return and then asynchronously invoke sqlTransactionCallback. Based on the result of sqlTransactionCallback, transactionErrorCallback or transactionSuccessCallback would be executed. Here is a code example:
In the above code, the transaction is invoked by defining only sqlTransactionCallback. The code does not have either transactionErrorCallback or transactionSuccessCallback in it. executeSqlThe executeSql method should be invoked as part of sqlTransactionCallback, transactionErrorCallback or transactionSuccessCallback of transaction or readTransaction. Here is the syntax:
The sqlStatement in the executeSql argument would be preprocessed using arugmentsWithinSquareBrackets. Here is a code example:
The nullDataHandler and the killTransaction are the sqlStatementCallback and sqlStatementErrorCallback functions, respectively.
Synchronous Database APIThe Synchronous database API has the interface DatabaseSync, which has the transaction and readtransaction methods. Here is the syntax:
Because the method is synchronous, the method would be executed immediately. Here is the executeSql syntax:
Database Query Results APIThe database query results API has the interface SQLResultSet. A successful execution with the executeSql method would invoke sqlStatmentCallback with SQLResultSet as an argument. SQLResultSet has three attributes for insertRowId, rowsAffected and rows. When the corresponding executeSql inserts a record into the table, the insertRow
would reflect the row ID of the last row to be inserted into the table. If
the corresponding executeSql is affecting any records, because of operations
such as The rows (third attribute) of type SQLResultSetRowList has length and item. As an example, consider the following code to understand the usage of SQLResultSet and SQLResultSetRowList.
In the above code, executeSql has an inbuilt sqlStatmentCallback. The callback method takes two arguments, namely transaction and results. Results is of type SQLResultSet, so Implementation of Shopping Cart Using Web SQL DatabaseIn this section, we walk through implementing a shopping cart application. When requested, the Web application will send the details of the products available to the client. To reduce the interaction between the client and the server, the client should store the details of the products, which include product ID, product name, qoh (quantity on hand), and price. When the user selects a particular product, the validation for the availability of the required quantity is carried out on the client. If the required quantity is not available, the application will pop up an alert. If the required quantity is available, the product will be added on to the local session variable, which acts as the shopping cart (see Figure 1). When the user confirms for billing, the products available in the local session variable can be further processed. Here are the procedures to be implemented for the problem:
The code to implement the above steps is explained below. Step 1The code below validates whether the browser supports the Web SQL database. If it does, the code uses the openDatabase method to create a database with the name productdatabase.
The initDB in the above given script should be invoked during onload of the Step 2The code below creates the table to store the product details provided by the server.
The method createTables should be invoked from the initDB given under step 1 when the database is created successfully. Step 3The code below is for displaying the list of products available to the user dynamically. The productList is of type ArrayList<Product>, where the Product has
Step 4aThe onclick of the button invokes the
The checkProducts method uses the asynchronous method of the Web SQL storage API. It is important to make sure that the validations happen one product after the other. Step 4bIf the required quantity is available, the product and the quantity required should be added on to the Web storage and the quantity on hand (qoh) should be updated accordingly. The updateProduct function delivers this functionality.
if the product already exists in the session, the function will add the current required quantity to the existing session variable. ConclusionIn this article, I explored the Web SQL database API and its usage by implementing a shopping cart application. AcknowledgmentsThe author would like to thank Raghavendran N, Principal, Education and Research Department (E&R) for his guidance and continuous support. I would like to thank Mr. Satheesha B N (AVP) and SVS (VP), E&R for motivating me to write this article. I would like to thank Mrs. Sangeetha for reviewing this article and providing valuable suggestions. About the AuthorYuvarani Meiyappan works as a lead in the E&R division of Infosys Technologies Limited. She has over eight years of experience in design and development of Java and Java EE applications. |