Fast response time is extremely important in the mobile world.
Mobile computing invites us into a whole new way of thinking. Web-based mobile programming is different for us in that it utilizes the various features of HTML5. One of the really great aspects of HTML5 is that you can run SQL in the browser without any connection to the server: Think airplane mode; think offline storage.
Airplane mode turns off Wi-Fi and cellular communications, leaving your mobile programs alone to fend for themselves without any contact with the outside world. This means no access to a remote server or any of its processes and databases.
It's true that not every program should function when the mobile device is in airplane mode. News sites like CNN, ESPN, and the like functioning in airplane mode would be like reading yesterday's news. Up-to-the-second data is what makes these sites meaningful.
On the other hand, there are several examples of mobile applications in which having access to hours-old, day-old, or even months-old data outweigh not having any access at all.
Imagine a sales-reporting application. In this case, last month's data is not going to change and is certainly valuable even if your mobile device has no access to its remote server.
Let's say your project manager tells you that the company's new Mobile Sales Reporting App needs to function when the salesperson is out of Wi-Fi range, at a remote customer site, or anywhere that connectivity is weak or nonexistent. In addition, there will be three months of past sales data resident on the device's local database. That's a lot to chew on. So how do we do it?
One of the very useful features of HTML5 is that you can store SQL tables in the browser. That's right. You can create and drop tables. You can insert, update, delete, and query rows just like you can with DB2/400 or any of your other favorite databases.
Let's discuss a few basics.
WebKit's use of HTML5 includes an implementation of the SQLite database. In addition to the plethora of mobile devices relying on WebKit, desktop browsers like Google Chrome and Apple Safari are also based on WebKit technology..
Here's a look at the developer tools within Google Chrome:
Figure 1: These are the WebKit developer tools in the Google Chrome version.
There's enough here to cover with an entire series of articles, but here are the basics:
- Elements—Review, debug, and modify styles while testing
- Resources—Review usage of SQL databases, local storage, session storage, cookies, and application cache
- Network—Review all items downloaded to the current page
- Timeline—Measure load times and performance of DOM events
- Audits—Measure and analyze load times
- Console—Review error messages, log messages, and code snippet execution
For this article, we'll focus on the Console and the Resources tabs.
Regardless of whether or not you need an airplane mode approach, utilizing SQL in the browser with your mobile app provides a powerful and fast way to access data on your mobile devices.
So how do we create and drop tables? How do we insert, update, delete, and query data in the browser?
Start your chrome browser, and then start the Development Tools.
Figure 2: Access Google Chrome's developer tools.
Now, select the Console tab. Type the following code snippet into the console:
alert('Check it out!!');
Press Enter. You'll see an alert box pop up on the page that's loaded into Chrome. See Figure 3.
Ok, so you've pasted some SQL code into the browser. You've created a table and inserted a record. Now let's see the results.
Click on the Resources tab. Expand the Web SQL tree.
Figure 5: These are the results of the SQL work.
You could run in airplane mode.
You could place static tables that rarely if ever change on the mobile device to minimize your mobile app's download time.
Remember: Fast response time is extremely important in the mobile world. And it's also the most difficult thing to achieve due to communications latency and connection availability. The more you can do to speed things up, the happier your users will be!
Here, I've given you enough to whet your appetite and get started. With a little creativity, you could solve a variety of problems using browser-based SQL. Download the code here.