25
Mon, Nov
1 New Articles

Mobile Development: SQL in the Browser

Development Tools / Utilities
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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?

Enter HTML5

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.

 

Nearly all of the mobile browsers that run on today's sophisticated devices (iPhone, iPad, Android phones and tablets, BlackBerry, and many others) are using WebKit as their underlying browser engines. WebKit includes processing for HTML5, CSS, and JavaScript.

 

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..

 

Note: A Web search of "sqlite html5 javascript" will reveal a number of valuable resources to take you deeper into the world of HTML5 SQL.

 

Mobile Web–application developers typically rely on desktop versions of Google Chrome or Apple Safari for testing their apps prior to deploying them to a mobile device. The simple reason is that Chrome and Safari's HTML5, CSS, and JavaScript rendering engines are basically the same as the rendering engines in use within your mobile device's browser. This makes Chrome and Safari particularly useful when developing and initially testing your mobile app.

 

Here's a look at the developer tools within Google Chrome:

 

012113SalisburyFig1

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:

 

  • ElementsReview, debug, and modify styles while testing
  • ResourcesReview usage of SQL databases, local storage, session storage, cookies, and application cache
  • NetworkReview all items downloaded to the current page
  • SourcesReview and debug JavaScript source
  • TimelineMeasure load times and performance of DOM events
  • ProfilesMeasure JavaScript and CSS performance (what's taking longest?)
  • AuditsMeasure and analyze load times
  • ConsoleReview error messages, log messages, and code snippet execution

 

For this article, we'll focus on the Console and the Resources tabs.

 

In addition to browser-based SQL, achieving airplane mode isolation in your mobile app will involve a couple of other issues. The code for your mobile app has to be cached in the browser (see this Sencha Touch tutorial for a brief and informative discussion on caching). Or your mobile app has to be built into a "native" app. It's worth noting that PhoneGap not only makes your HTML/CSS/JavaScript app into a native app for iPhone, Android, BlackBerry, etc., it also gives you access to the mobile device's native functionality, like the camera, contacts, accelerometer, and so much more. PhoneGap provides native device functions not directly available to JavaScript or HTML5.

 

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?

 

Answer: JavaScript.

 

Let's take a quick tour of how we can manage SQL with JavaScript using the Google Chrome developer tools.

 

Start your chrome browser, and then start the Development Tools.

 

012113SalisburyFig2

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.

 

012113SalisburyFig3

Figure 3: Test live snippets of JavaScript.

 

In Figure 3, you see how you can simply enter JavaScript into the console and test it out. This is a common exercise when writing JavaScript code, and it provides an easy way to test code snippets before committing changes to your actual JavaScript files.

 

Now that you see how to process JavaScript on the fly with WebKit's developer tools, let's do something a little more interesting. Let's write some JavaScript that utilizes SQL.

 

012113SalisburyFig4

Figure 4: Process some SQL via JavaScript in the WebKit/Chrome browser's console.

 

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.

 

012113SalisburyFig5

Figure 5: These are the results of the SQL work.

 

The mcpress database containing the Customer table is available to the JavaScript application. With the approach shown here, you could load data down from your server once and run against the local (browser) copy of the database without having to communicate with your server (think faster response times).

 

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.

Scott Salisbury
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: