HTML and CSS Reference
In-Depth Information
Be very wary of versioning!
Currently, the implementations of Web SQL Databases support a
slightly older version of the Web SQL Databases API, and more spe-
cifi cally the versioning model.
Although the specifi cation describes how you can manage and
migrate from different versions of the database, this hasn't been
implemented very well. The model requires that you know the ver-
sion of the database on the user's machine to be able to open it.
The problem is that if you have migrated through multiple versions of
your database, there's no way to determine which version the visiting
user is on, and opening the database with the wrong version number
throws an INVALID_STATE_ERROR . You could wrap each of the open
database attempts in a try/catch, but you'd required one for each ver-
sion of your database, something that could get very messy after a
few years of upgrades.
Using the Web SQL Database API
The typical database API usage involves opening the database
and then executing some SQL. Note that if I were working with
a database on the server side, I would typically close the data-
base connection. This isn't required with the database API, and
in fact there's no method to do this at all. That all said, you can
open a database multiple times.
Opening and creating databases
By opening a database for the fi rst time, the database is cre-
ated. You can only have one version of your named database
on the domain at any one time, so if you create version 1.0 you
can't then open 1.1 without the database version having been
specifi cally changed by your application. For the rest of this
chapter, I'm going to ignore versioning and stick to one version
only due to the previously stated warning.
var db = openDatabase('mydb', '1.0', 'My first database',
¬ 2 * 1024 * 1024);
The latest version of the SQL databases spec includes a fi fth
argument to openDatabase, but this isn't supported in any of
the browsers right now. It offers a callback when the database
is created for the fi rst time. You've now created a new data-
base called “mydb,” version 1.0, with a text description of “My
Search WWH ::

Custom Search