Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com�s entire library of video tutorials.
The Execute an SQL Query. This is the query, Select Count of everything from the database table name. What this does is it's simply counting the number of rows in the table and the purpose for that is to find out if the table exists. We know at this point that the database exists, but within the database can be one or more tables. Now, DB table name, I can scroll up here. I could search for it up here. The DB table name is defined up here as simple key value table and I'll talk about key values a little bit later, but this is the name of the table I'm looking for. I've got the database here either created or opened and now what I do is I come in here in the Transaction and the first thing that I do, this is going to execute the tx function and right down here, we call tx.executeSql. Here is the query and it says "Select Count from the Table Name," which was defined up there. It's Simple Key Value Table. We will try to execute that. We'll skip over the square brackets for the moment. This is the function that is called "If Things Succeed". If there's an error, this is the handler that is called. Now, everytime you see the executeSql function here, you're going to look for up to 4 parameters. The first is the query. The second is the square brackets as the parameters to the query. The third is the handler to call if it succeeds and the fourth is the handler to call if it fails. So, you will have a query, possibly parameters, handler for success and a handler for failure. And that's true of executeSql in every case in which it occurs, although the last 2 handlers need not appear. So, what we've done, we've attempted to count the number of rows in the simple key value table and we call this if in fact the number of rows has been found. If it hasn't been found, we can't count it. What that means is that the table has not been found. So we need to create it and this is the comment in the template, but it's actually incorrect. The actual comment should be "The table doesn't exist," let's create one because we only got into this block if the database exists. So, the error handler creates a table with that table name and it contains Ñ whenever you create a table, you have to create at least 1 column and that's created here. We'll come back to this a little later on, but what's interesting here is that remember the 4 things in executeSql. The query, square brackets, the success handler and the failure handler. Now, the failure handler here has its own executeSql call, which has its own query. This time, it's a Create Table query, the square brackets. It has no exception handler, but it does have here the handler for If It Succeeds and lo and behold, the code in the success handler here for creating a table turns out to be the same as the code in the Select Count row counting handler up here. In either case and there are some extra "if" statements here, but don't worry about that. In the successful handler, we call initContent. We call initContent here and then we call loadContent and we call loadContent here. So, either by accessing that Simple Key Value Table here, we winded up calling initContent and loadContent or by creating it, we'd call initContent and loadContent. And at that point, what we do is we drop down here and return to database. So that's the beginning of initDB.
| Course: | Developing iPhone Web Apps |
| Author: | Jesse Feiler |
| SKU: | 34075 |
| ISBN: | 1-935320-89-0 |
| Release Date: | 2009-12-31 |
| Duration: | 8 hrs / 103 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |