HTML and CSS Reference
In-Depth Information
fi r s t d a t a b a s e , ” a n d y of u ' v e s e t t h e s fi z e of fi t h e d a t a t of 2 M b ( t h fi s
has to be set in bytes which is why I multiply 2 * 1024 * 1024).
To e n s u r e To u r a p p w To r k s a n d d e t e c t s s u p p To r t f To r t h e We b S Q L
database API, you should also test for database support in the
browser, so you wrap our code with the openDatabase test:
var db;
if (window.openDatabase) {
db = openDatabase('mydb', '1.0', 'My first database',
¬ 2 * 1024 * 1024);
}
It's as simple at that. The next thing to do is set up a new table
in the database, which will go through the exact same methods
as selecting and updating tables: via executeSql .
Creating tables
With creating tables (and any other transaction on the database),
you must start a database “transaction” and, within the callback,
create the table. The transaction callback receives an argument
containing the transaction object, which is the thing that allows
you to run SQL statements and run the executeSql method ( tx in
the following example). This is done using the database object
that was returned from openDatabase and calling the transaction
method as so:
var db;
if (window.openDatabase) {
db = openDatabase('tweetdb', '1.0', 'All my tweets',
¬ 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE tweets (id, date, tweet)');
});
}
The executeSql method takes four arguments, of which only the
fi r is t fi is r e q u fi r e d :
1.
SQL
2.
Arguments to SQL (such as fi eld values)
3.
Success callback
4.
Error callback
 
Search WWH ::




Custom Search