25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 9-11. sql.html displaying the results of SELECT * FROM racers<br />

CHAPTER 9 ■ USING THE <strong>HTML</strong>5 WEB STORAGE API<br />

Database operations can take some time to complete. Instead of blocking script execution until a<br />

result set is available, queries run in the background. When the results are available, a function given as<br />

the third argument to executeSQL() is called back with the transaction and the result set as arguments.<br />

Listing 9-13 shows the complete code for the file sql.html; the sample code shown is also located in<br />

the code/storage folder.<br />

Listing 9-13. Using the Web SQL Database API<br />

<br />

Web SQL Database<br />

<br />

// open a database by name<br />

var db = openDatabase('db', '1.0', 'my first database', 2 * 1024 * 1024);<br />

function log(id, name) {<br />

var row = document.createElement("tr");<br />

var idCell = document.createElement("td");<br />

var nameCell = document.createElement("td");<br />

idCell.textContent = id;<br />

nameCell.textContent = name;<br />

row.appendChild(idCell);<br />

row.appendChild(nameCell);<br />

}<br />

document.getElementById("racers").appendChild(row);<br />

function doQuery() {<br />

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

tx.executeSql('SELECT * from racers', [], function(tx, result) {<br />

// log SQL result set<br />

for (var i=0; i

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

Saved successfully!

Ooh no, something went wrong!