Monday, August 1, 2011

iOS 5 Tutorial - Part I. SQLite + UITableView + UISearchBar Tutorial

     There are many ways to retrieve and store data on the iPhone, from property lists to NSCoding, from SQLite to Core Data. I thought that after trying to learn all three that SQLite was the easiest to learn. This three-part tutorial series is going to cover how to fill a UITableView with data from a SQLite database and then incorporate a UISearchBar. I really thought there were no good tutorials on all three so I thought I should create my own. 


     In this first part of the tutorial we will create a sqlite database and then read the values from the database and create an array of objects to fill the tableviews in the future tutorial. This first tutorial can really apply to anything where you have to read from a database and then create an array of objects.


1. Suppose you have a csv file of buildings with the fields id, name, latitude, longitude, type. You will need to convert this to a sqlite database. Place the buildings.csv file on the desktop. 







2. Open a terminal window and with the buildings.csv file on the desktop execute the following commands. 






3. Then you should see something like the following. 






Note: I converted everything to type text. I thought it was much more difficult later on to convert number types and everything is a lot easier to deal with that way. 


4. Create a new Master-Detail Application called buildings. Then drag and drop the buildings.sqlite file into your project folder. Then add the libsqlite3.dylib framework to your project. Click the buildings project at the top of the far left column. 


i.e. buildings > Building Phases > Link Binary With Libraries > + > libsqlite3.dylib 










5. For organizational purposes drag and drop the .dylib file into your frameworks folder. 


6. Then create a new building object called Building which is a subclass of NSObject. 






7. Now go to your buildingsAppDelegate (.h/.m) and add the code below.





8. Now with the added code you should NSLog the values from the values retrieved from the database. Most of the code is commented/ self-explanatory so I will not go into depth about what everything does here. 


It's important to correctly NSLog the values to ensure that you are doing it correctly. Now that you've read from the database correctly you can now move on to filling the UITableView. 


Note: The first battle is to ensure that your program is correctly reading from the database. If you can retrieve the values from the database and the objects look like they make sense, then you can move on. 


Debugging Tip #1: Print out the values that were retrieved from the database.


Debugging Tip #2: Print out the values in the fields of the objects. Sometimes objects are not allocated or set properly and this will save you a lot of headache in the future.  


My Error 1:


Code: NSLog(@"Name: %@, Latitude:%g Longitude: %g", name, latitude, longitude);


Discussion: Since the variable type in my object was of type NSString the output was incorrect since I used (%g) which is for doubles. 




Debugging Output 1: This is what my output looked like when I printed the values that were retrieved from the database.




Debugging Output 2: This is what my output looked like when I printed the values that were retrieved from the objects in the items array. 















179 comments :