We will be undergoing scheduled maintenance on May 20th, 2013 at 02:00 GMT.
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.
Let's continue to drill down and we want to go to insertValue and here is the insertValue Function. It's in Utility Support so it's a slightly lower level function and what this means is that it's here because it's part of the template and I'm unlikely to change it because it's in Utility Support. It's not a hard and fast rule, but it's true that most of the time, I'm not going to change it and you can use insertValue like this without changing it for many, many apps that you'll write and it has the same structure that we've seen before. We checked to see if the database exists and then we create a transaction using this function and we call it, executeSql and remember I pointed out, there were four parts to the executeSql call. There's the query. I always skipped over square brackets before but now they're here and then there may be a success handler and there may be a failure handler. In this case, we have the square brackets, we don't have either a success or a failure handler. We just go ahead and do it and what this is going to do and you can see from the comment, it inserts into the database those three value; ID, key and value. They were things like 0 message and the actual message value and here is where those square brackets come into play. The SQL query is specified here. It is going to turn into?? remember the DB table name is defined way up at the top here inside Utility Support. This is the Simple Key Value Table. Let me go back down to insertValue and so we're constructing the query. It will say, "Insert into Simple Key Value Table," and then this is plain old SQL syntax, the columns that are being set, ID, key and value and then the values and there are three question marks here and they correspond to what's in the square brackets and that's where the square brackets come into play and what's in the square brackets are ID, key and value, which could be local variables. In this case, they are the three parameters that were passed into insertValue, so this is going to wind up being Insert into Simple Key Value Table ID, key and value, the columns to be updated, values, plain old SQL syntax the value of ID, the value of key and the value of value, so it's a simple, very simple ordinary vanilla, SQL statement and that's how it's constructed here and that's how we are going to insert the value and that's how every value is going to be inserted. Now, while we're here, we might as well look at Update Value, which happens to be right next to it in the source file and it has the same three parameters. If the database is there, then we create the transaction and the SQL for this transaction is Update the Table Name and we are setting the key and the value where and this is the syntax for updating where ID equals something and what's going to happen is, here ID equals and then we are using the concatenation operator so ID, which was passed in the value will be placed here so it will say where ID equals 0 or 3 or whatever, what the value is here, but I still have two question marks here in the syntax and the replacement values, the parameters that are passed in will be placed in this square brackets so the first question mark will be set to the value of key because it's here, which is in fact the second parameter, value equals question mark we replaced with the actual value that was passed in for value. It's actually the same logic as here, but the syntax is very slightly different because I have the where clause in there. This is the way in which all of the values are inserted and updated. It's the plain, ordinary, everyday code and notice that because this is in Utility Support, nothing is specific. All we have is ID, key and value and those are determined when you call it from the main JavaScript file, so you don't need to change these functions at all. The only thing, as I mentioned, that you might want to change here is the actual name of the database. The table, the Simple Key Value Table is neutral. It doesn't matter what you're dealing with, but what you might want to do is to change the name of the database depending on what your app is dealing with. So believe it or not, we have now gone through, in fairly good detail, the entire database structure and operations for the Utility Template, which is a basic template that provides a user interphase with a widget that flips front and back and that stores data in the local database that is built in.
| 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 |