Jump to content

How to Use HTML 5 Storage in Palm webOS

+ 1
  steve@oreilly.com's Photo
Posted Nov 19 2009 04:21 PM

Palm webOS supports the HTML 5 Database object. The following excerpt from Palm webOS shows you how to create and query an SQL database using the HTML 5 Database object.


The HTML 5 specification includes extensions for structured client-side storage, including support for the Storage and Database objects. Palm webOS does not support the Storage object, a list of name/value pairs that grew out of Firefox’s DOM Storage object, but it does support the Database object.

The openDatabase() method will create a new database or open an existing database, returning a Database object:

this.db = openDatabase("myDB", 1, "My DB", 10000);

The arguments are as follows (see Table 6.1).

Table 6.1. openDatabase arguments

Property

Required

Description

Name

Required

Database name

version

Optional

Target version, or undefined if any version is acceptable

displayName

Optional

Application defined, not used by webOS

estimatedSize

Optional

Informs webOS of intended size to prompt for any system constraints at creation rather than during use


The database version property is meant to represent the schema version, enabling smooth migration of the database forward through schema changes. If the application specifies a database name and a version number, both need to match an existing database for the database open to succeed; otherwise, a new database will be created.

Once the database is open, you can execute transactions against it, using either transaction() for read/write transactions or readTransaction() for read-only transactions. The transaction methods will specify one to three callbacks:

  • Transaction callback

  • Error callback

  • Success callback

The transaction callback is the most important; it includes the transaction steps that you wish to execute using an executeSQL() method, which accepts an SQL query string as an argument along with success and error callbacks.

For example, the following code segment calls the transaction method with a literal function that includes two executeSQL() methods, the last of which specifies a success callback, this.successHandler(), and an error callback, this.errorHandler():

MyAssistant.prototype.activate = function()    {

          .

          .

          .

this.db.transaction( (function (transaction) {

            transaction.executeSql('A BUNCH OF SQL', []);

            transaction.executeSql('MORE SQL', [], this.successHandler.bind(this),

               this.errorHandler.bind(this));

     }).bind(this);

          .

          .

          .

MyAssistant.prototype.successHandler = function(transaction, SQLResultSet) {

// Post processing with results



};



MyAssistant.prototype.errorHandler = function(transaction, error)  {

    Mojo.Log.Error('An error occurred',error.message);



    // Handle errors here

};

The success handler is passed the transaction object plus an SQLResultSet object as an argument. The attributes of the results object are described in Table 6.2.

Table 6.2. SQLResultSet object

Attributes

Description

insertID

Row ID of the row that was inserted into the database, or the last of multiple rows, if any rows were inserted

RowsAffected

Number of rows affected by the SQL statement

SQLResultSetRowList

Rows returned from query, if any


We’ve only touched on the basics of the HTML 5 database capabilities here. As a draft standard, HTML 5 will continue to evolve. You should review the formal SQL reference at http://dev.w3.org/ht...rage/#databases for more in-depth information. There’s also a basic demo application at http://webkit.org/de...otes/index.html.

Palm webOS

Learn more about this topic from Palm webOS.

This is the official guide to building native Javascript applications for Palm's new mobile operating system, Palm® webOS™. Written by Palm's software chief technology officer along with the Palm webOS development team, Palm webOS offers you a complete tutorial on the design principles, architecture, UI, tools, and services necessary to develop webOS applications. If you're familiar with HTML, CSS, and Javascript, you're ready to build applications for webOS-based devices, including the Palm Pre.

See what you'll learn


Tags:
0 Subscribe


0 Replies