HTML and CSS Reference
In-Depth Information
Web SQL Databases
Web SQL Databases are another way to store and access data.
As the name implies, this is a real database that you are able to
query and join results. If you're familiar with SQL, then you should
be like a duck to water with the database API. That said, if you're
not familiar with SQL, SQLite in particular, I'm not going to teach
it in this chapter: There are bigger and uglier topics that can do
that, and the SQLite website is a resource at http://sqlite.org .
The specification is a little bit grey around the size limits of these
databases. When you create a new database, you, the author,
get to suggest its estimated maximum size. So I could estimate
2Mb or I could estimate 20Mb. If you try to create a database
larger than the default storage size in Safari, it prompts the user
to allow the database to go over the default database size. Both
Opera and Google Chrome simply allow the database to be cre-
ated, regardless of the size. I strongly suggest that you err on the
side of caution with database sizes. Generally, browsers are limit-
ing, by default, databases to 5Mb per domain. Now that you're
suitably worried about SQL and database sizes, one really neat
feature of the Web SQL Databases API is that all the methods
allow you to pass a callback that will be executed once the fan-
dango SQL magic has completed. Callbacks are a common trait
of JavaScript libraries such as jQuery. If you're not familiar with
this syntax, it looks something like what follows (but don't worry,
I'll hold your hand throughout the examples later on):
transaction.executeSql(sql, [], function () {
// my executed code lives here
});
Due to the nature of the callback system, it also means that the
database API is asynchronous. This means you need to be care-
ful when authoring the JavaScript to deal with the database to
ensure that the sequence of events runs correctly. However,
the SQL statements are queued and run in order, so this is one
slight advantage you have over processing order: You can cre-
ate tables and know that the table will be in place before you
run queries on the tables.
Put plainly, if you want your code to run after the database
interaction has completed, use the callback. If you don't need to
wait, and you want your code to continue regardless, continue
after the database API call.
note Mozilla and Micro-
soft are hesitant about
implementing SQL database
support. Mozilla is looking at a
specification called Indexed
Database API which is only in
early betas of Firefox 4 and is
beyond the scope of this chap-
ter. But it's worth keeping an
eye on it in the future.
Search WWH ::




Custom Search