HTML and CSS Reference
In-Depth Information
Web SQL Database
Web SQL Database is 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 take like a duck to water with the database API. That
said, if you're not familiar with SQL, and 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 good starting point.
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 you could
estimate 2 MB or you could estimate 20 MB. 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 Chrome simply allow the data-
base to be created, regardless of the size. I strongly suggest
that you err on the side of caution with database sizes; as I said
earlier, browsers limit databases to 5 MB per domain by default.
Now that you're suitably worried about SQL and database sizes,
one really neat feature of the Web SQL Database API is that all
the methods allow you to pass a callback that will be executed
once the fandango 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 this (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, so you need to be careful 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 in which they were
queued, so this is one slight advantage you have over process-
ing order: you can create 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.
Search WWH ::

Custom Search