Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

Lesson 2: Working with IndexedDB CHAPTER 16 589


■■Schema migration support is available by using the changeVersion() method.
■■Web SQL supports a common SQL syntax for create, retrieve, update, and delete
(CRUD) operations.
■■If one statement in a transaction fails, all actions are rolled back.

Lesson review


Answer the following questions to test your knowledge of the information in this lesson. You
can find the answers to these questions and explanations of why each answer choice is correct
or incorrect in the “Answers” section at the end of this chapter.


  1. Which of the following would be a good candidate for Web SQL?
    A. Mobile applications built specifically for Safari on the iOS platform
    B. Mobile applications built for any mobile device
    C. Public-facing web applications
    D. Mobile applications built specifically for Internet Explorer

  2. You need to create a new database. Which of the following commands should you use?
    A. var db = createDatabase(‘mydb’, ‘1.0’, ‘My database’, 5 1024 1024);
    B. var db = new Database(‘mydb’);
    C. var db = initDatabase(‘mydb’, ‘1.0’, ‘My database’, 5 1024 1024);
    D. var db = openDatabase(‘mydb’, ‘1.0’, ‘My database’, 5 1024 1024);

  3. Which of the following will correctly insert a new record, using values passed in as SQL
    arguments to an executeSql() call?
    A. t.executeSql(“INSERT INTO books(title) VALUES(?)”, [“A Tale of Two Cities”]);
    B. t.executeSql(“INSERT INTO books(title) VALUES([0])”, [“A Tale of Two Cities”]);
    C. t.executeSql(“INSERT INTO books(title) VALUES([1])”, [“A Tale of Two Cities”]);
    D. t.executeSql(“INSERT INTO books(title) VALUES({0})”, [“A Tale of Two Cities”]);


Lesson 2: Working with IndexedDB


So far, you’ve seen two extremes for client-side data storage. Web storage provides a simple
key/value persistence model but lacks some of the features that are important when working
with a database. The other extreme, Web SQL, provides many of the features associated with
a fully functional relational database but brings with it all the manual work required for set-
ting up and maintaining the persistence structure.
IndexedDB provides a compromise between the two alternatives. It’s a key/value database
in which values can range from simple strings to complex object structures. To provide for
fast retrieval and searching, it includes an easy way to create indexes for each of your object
Free download pdf