04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 21: Upcoming API s<br />

Database Storage<br />

HTML 5 introduces a client - side database storage system that is accessible from JavaScript. The first<br />

browser to implement this feature was Safari 3.1; no other browsers have implemented it at the time of<br />

this writing. To start using client - side database storage, you ’ ll need to call the openDatabase() method<br />

on the window object. This method accepts four arguments: the database name, the database version, a<br />

display name, and the estimated size of the database in bytes. If the database already exists, then<br />

openDatabase() returns a Database object representing the existing database; if the database doesn ’ t<br />

already exist, openDatabase() first creates the new database and then returns a Database object for it.<br />

Here is an example:<br />

var db = window.openDatabase(“Test DB”, “1.0”, “My Test Database”, 200000);<br />

The primary method of interacting with a database is the transaction() method. This method accepts<br />

three arguments: a transaction callback that is executed when the system is ready to work with the<br />

database, an optional error callback, and an optional success callback. All database operations are<br />

executed asynchronously, which is why each argument to transaction() is a function.<br />

A transaction callback function receives a SQLTransaction object as its only argument. This object has<br />

only one method, executeSql() , which accepts four arguments: the SQL string to execute, an optional<br />

array of parameters to embed in the SQL, an optional success callback, and an optional error callback.<br />

The success callback receives two arguments: a SQLTransaction object and a SQLResultSet object<br />

containing any results. The error callback receives a SQLTransaction object and an error object<br />

indicating that the error that occurred.<br />

To begin using a client - side database, you ’ ll need to create a table as follows:<br />

db.transaction(function(transaction){<br />

});<br />

transaction.executeSql(“CREATE TABLE Messages (id REAL UNIQUE, msg TEXT)”, [],<br />

function(transaction, results){<br />

//database was created<br />

},<br />

function(transaction, error){<br />

//database wasn’t created<br />

}<br />

);<br />

This code creates a simple table named Messages that has two fields: the primary key id and msg to<br />

hold some text. The second argument is an empty array because it ’ s not necessary for the completion of<br />

the database operation. If the creation is successful, then the success callback is executed. There won ’ t be<br />

any results for this operation, so the callback can just be used to indicate that the database was created as<br />

expected. If the database isn ’ t created, then the error callback is executed and you can use the error<br />

argument to determine what happened.<br />

Since there isn’t a way to simply detect the existence of a database table, you may<br />

need to first run a query against the table and, if an error occurs, assume that the<br />

table doesn’t exist.<br />

694

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!