Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
At this point we have started to incorporate BWDB in the Testbed code for testing. We've set up the SQLite 3 Framework in Xcode, and we've imported the BWDB Library into our Testbed app. Now, it's time to test the library in the testbed. So, you're going to take the results from your last exercise or if you prefer, you can use Testbed-02-done, which is my version. And I'm going to make a working copy of this. And I'm just going to call it Testbed-03-working, and so this way we can pick up where we left off at the end of the last movie.
And I'm going to double-click on the Testbed. xcodeproject inside of that folder to open the project in Xcode. So, we have our BWDB Library, there is the m file, and there is the h file. I'm just going to close that up for now, we go to our BWUtilities. And I'm going to come over here to the TestbedViewController.m file, and up here at the top I'm going to import the BWDB header. And I'm going to come back out here into our finder, and I'm going to take this BWDB testbed code and open that up in my text editor. And we've got a couple of methods here.
I'm just going to copy these and paste them into my m file. So, Command+C to copy, coming back here into Xcode, and down here in the implementation I'm going to paste those in, and there they are. And you see that our method here is called testDatabase. And so, I'm just going to come down here in viewDidLoad and instead of runTest, I'm just going to type in testDatabase like that.
And so runTest here is actually often we can leave it there, or you can delete it, it's up to you. So, let's just go ahead here and run this and then we'll come back, and we'll look at the code. Make sure you select the iPhone Simulator and press the Run button, Build Succeeded, that's always a good sign. And so there is the Simulator, we see we have our BWDB version, SQLite version, create table, and all of these database operations happening successfully.
So, let's take to look at the code. So, there's a table name and a file name. And there is an actual BWDB object being declared. And here we have our alloc init pattern, and we're initializing with the file name and the table name. And we call getVersion to display the version, and there is our version there. And SQLite version is value from query select sqlite_version, so there is the sql query to get the SQLite version, and that's getting displayed here.
So, that actually tells us that a lot of stuff is working here. Our interface to the SQLite Framework is working and the interface to BWDB is working. So, we're actually able to send an SQL query and get a value from that, and that's a really good sign. So now, we come out here, and we create a table. You notice I have this message call to create--to display the create table string. And doQuery is the version of the query code, and if we come in here to the BWDB header and SQL query's doQuery simply returns a number of rows that were affected, if any.
So, it's typically called without testing the return value. And we come back down here, and we can see doQuery here has been called without testing the return value. Drop table if exists and then CREATE TABLE. We're creating a table with an id column, INTEGER PRIMARY KEY that's SQLite for an auto incremented integer key. SQLite allows you to create columns without a type because it's really a very loosely typed thing anyway.
And stamp is TEXT DEFAULT CURRENT_TIMESTAMP and so it will always put a timestamp on every single row. And so, now we're going to go ahead and insert a row, and we insert into BW table, so we're using doQuery for this insert, and we return the lastInsertId, and we see that we get one for that. And then using CRUD to insert with insertRow, and these are literal NSDictionary types.
So, we have key value, key value, key value, that's a new feature in Objective-C, and it's much more convenient than the old way. So, I tend to use these literal values a lot. So, you see I'm using some different types here, I've got the literal floating point, I've got literal integers and here is a literal string. So, NSNumber doubles, integers and Mixed. And you see, when we look at these values, we get the return values based on the way that SQLite handles these sorts of things.
And here I'm reading it back, and that's where it's displayed here using the NSFastEnumeration. And so, I simply pass the query into the db object and for a row in, the value that's returned by that is the actual db object. If we look at the getQuery code over here, we notice that it returns itself. So, that's actually returning the BWDB object. And because of NSFastEnumeration, I'm able to just say for row in that object, and then call display row with that row.
And display row of course is up here, and it's just this simple message with the row. And again, we're using this new subscripting feature that's new in Objective-C. So, we use db countRows here, and there we have count rows 4, and then we delete a row and then we get count rows 3. So here, we delete row three with deleteRow, and we call countRows again we get a different value. And then we look, and we have just row 1, row 2 and row 4, and that again is using the NS enumeration for displaying the rows in the table.
And then we're able to display an individual row using getRow. So, this should make it clear that the value of this wrapper is it makes it easy to work with SQLite in an Objective-C environment. We are using Objective-C paradigms for all of this, rather than having to write C code in the middle of our Objective-C. It makes this all seamlessly integrate with our Objective-C code for iOS. It's also easy as we'll see in another lesson in this chapter to subclass BWDB and create a specific interface for a specific application.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98382 Viewers
61 Video lessons · 85669 Viewers
71 Video lessons · 69603 Viewers
56 Video lessons · 101928 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.