As of planning Firefox 2 the Mozilla team searched for ways to "provide a unified interface for storing and searching through data for all Mozilla components and extensions" [1]. This should also eliminate multiple file formats that have been in use for Mozilla profiles and settings and "provide Mozilla core components and extension authors with tools to enable richer interaction with user data" (ibid). The solution they came up with was mozStorage [2], a database API for C++ and Javascript with SQLite, an "in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine" [3] on the backend side. Browsing your Mozilla profile directory will reveal these .sqlite files that take care of history, bookmarks, cookies, form or search history, download manager bits and the like.
This paper will show how to make us of mozStorage to create offline clientside, database driven applications with Javascript, SVG and SQLite. It's targeted at SVG developers with a sorrow knowledge of scripting and beginning to intermediate knowledge of relational databases, respectively the use of SQL.
As this example demonstrates clientside database storage it must be run locally. Please download hello.tar.gz, unpack it and open hello.svg with Firefox 2 or 3. To demonstrate the basics of clientside database usage let's start with a simple "Hello SQLite world" application that accomplishes the following tasks:
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
. This will prompt the user to grant the required permissions. Be sure to check "remember decision" as this will prevent you from being prompted whenever you execute statements.
onclick
eventListener to a text-element that is used to display the current database content. This text is set onload
to the value of column msg in table hello. Clicking it prompts for a new string that is to be written to the msg column instead.
Extending the concept of our simple Hello SQLite World example, it is possible to build more complex applications like the one being presented in the "Tasmania SQLiteSVG workshop proposal". It shows how to create a map for Tasmania with geometry layers, configurable annotation and queryable thematic data coming solely from SQLite (see screenshot).
The concept of clientside database storage as chosen by Mozilla way back when they designed Firefox 2 is by no means unique. Goggle gears provides a similar mechanism [6] and even more important than that, the WHATWG working group has added a "Clientside database storage" section to the HTML5 specification draft [7]. This will standardize the API and help to harmonize different approaches for solving the same task in the future. Let us explore these new possibilities!
Page: http://svg.cc/sqlite/abstract.html
Author: Klaus Förster <klaus.foerster@svg.cc>
Version: $Id$