So you’re trying to hack together some offline capability for your browser-based app, good for you! Until HTML 5 came along, there were basically two options to accomplish this: you could either require your users to install Google Gears or you could distribute a local server stack. Enter HTML 5′s offline capabilities!
Now, besides Cookies, there are three new offline capabilities of note: an application caching mechanism (including the option of declaring a self-contained applet using the manifest attribute), local object storage and local SQL databases. We’re gonna be talking about the latter. Offline SQL is currently available in Safari, Google Chrome, on the iPhone and Palm’s WebOS (both for its applications and browser-based content). As of now, Firefox 3.51 seems to have basic support, but its implementation is either nonstandard or so buggy as to be unusable.
Speaking of standards, there is no clear spec on the actual SQL dialect used for HTML 5. Instead SQLite is considered the reference implementation. That means if you got any questions about your actual SQL code, head on over to the SQLite site.
Accessing the local database
There is an easy way to test whether your environment supports HTML5 database:
if (window.openDatabase)
db = window.openDatabase("app", "", "my app db name", 1024*1024);
Now you can simply test if db is an object or not after this operation. If you got a database object back that means your browser has support built in and it successfully accessed your app’s storage. Here’s how it works:
window.openDatabase( DatabaseName, DatabaseVersion, DisplayName, EstimatedSize )
Be careful with the DatabaseVersion parameter though. If it’s set and it doesn’t match, the operation will fail. This gives you the opportunity to implement an upgrade mechanism but it can obviously also cause a lot of headache.
Executing queries
The database interface provides asynchronous access through a transaction paradigm:
transaction.executeSQL( SQLStatement, SQLParameters, ResultsetCallback, ErrorCallback )
…and this is how it looks in action:
db.transaction(function(tx)
{
tx.executeSql('SELECT * FROM MyTable WHERE CategoryField = ?',
[ selectedCategory ],
function (tx, rs) { displayMyResult(rs); },
function (tx, err) { displayMyError(err); } );
});
This fetching example shows how to get data from the DB, or more generally: how to execute arbitrary SQL statements. In this example displayMyResult(rs) and displayMyError(err) are just placeholders for whatever you want to do with the result once it arrives.
That’s pretty much it for the basics, now you can write your own offline app!
Thanks for posting this. Can you show how you access field objects in rs?
hey, thanks, this is a great information, could you show us a code in which you retrieve a register info from the rs var
Where is the physical location of the database (.db)? I have tried doing the above and then searching for the .db. What I’m really interested in is where do I put an existing database that I want to use?
hi
i have created sqllite database(question) and i save this my desktop. now i want to access(question) using html5 pls help me i am new in html5 its urgent.
thanks in advance
The info here is out of date. Check out one of the myriad HTML5/SQL tutorials: http://html5demos.com/