IMPORTANT:
The following tutorial has been deprecated and should not longer be used for any purposes. To learn more about on how to use SQLite using iphone applications click here

Welcome to the second part of the tutorial, where we will select a coffee house and display all the coffee it sells (in a table view). We will also be able to select a coffee and see its details in a grouped table view. This is how the application will look like

Welcome to the second part of the tutorial, where we will select a coffee house and display all the coffee it sells (in a table view). We will also be able to select a coffee and see its details in a grouped table view. This is how the application will look like

I do hope this tutorial will answer some of the questions that you have. Do expect a personal email from me sometime soon, answering all the questions that were asked.

In this tutorial

  1. Changes from Part I to Part II
  2. Loading another table view
  3. Creating parametrized queries
  4. Creating a grouped table view
  5. Reloading data


DetailView and DetailViewController has been deleted. Class Coffee has been renamed to CoffeeHouse and coffees array has been renamed to coffeeHouseArray.

Create a new view called CoffeeView and CoffeeViewController. Place a table view on the CoffeeView and create a variable in the header file of CoffeeViewController to connect to the object on the view. We also need to implement the UITableViewDelegate and UITableViewDataSource protocol, so we can handle the events generated by the table view and let the table view know that this is the datasource.

Set the File’s Owner proxy object class to CoffeeViewcontroller and create your appropirate connections.


We then need to handle the didSelectAtRowIndexPath method in the root view controller to load the coffee view and pass the coffee house information to the coffee view. To pass the coffee house object we create a variable of type CoffeeHouse (name it aCoffeeHouse) in the coffee view controller. We set this variable in the didSelectRowAtIndexPath method. We implement the set method for this variable, where we first assign the value to the local variable and then get the list of coffees for the coffee house and add it to the array.

Our setACoffeeHouse will look something like this


setACoffeeHouse calls a method getListOfAllCoffeesForThisCoffeeHouse which takes an int. We now query the database to get all the coffee’s for a coffee house. Since compile a sql statement is a little costly we will reuse the compiled statement by creating a static variable of type sqlite3_stmt and use the following sql string “select id, coffeename, size, price, contents from coffee where coffeehouseid=?” to prepare the statment. We will substitute the ? with the coffeehouseid parameter using the sqlite3_bind_int method. We have to remember that parameters in sql start from 1 and not 0 like the array in objective-c. The rest is simple, we get the data from the columns and create a new object of type coffee detail, set all its values and add it to the array.

We now tell the table view how many rows to expect by returning the count of the array. Next thing to do is to get the object from the array in cellForRowAtIndexPath and get its coffee name and set the text property of the cell.


Create another view and name it CoffeeData, then add a table view on top of it and change its style to “Grouped”. Save the view and move the nib file under the resources directory. Create another view controller and name it CoffeeDataController and implement the UITableViewDelegate and UITableViewDataSource. Create a variable of type UITableView with which you can connect to the table view object. Set the class to the File’s Owner Proxy object and create all the connections. In the implementation file we need to implement all the methods required. We will use this class to display all the data releated to the coffee.

Befor we display the data, we need to load the coffee data view when a coffee is selected. To do this create a variable of type CoffeeDataController in the header file of the CoffeeViewController and in didSelectRowAtIndexPath method, we will create an instance of the view we will load and add the view to the top of the stack of the navigation controller.

In the implementation file of the CoffeeDataController, instead of returning 1 from numberOfSectionsInTableView method, we will return 4 one section for one of the pieces of data coffeename, prize, size and contents.

We also need some title for the four sections we just created. To add the title, we need to implement another method called titleForHeaderInSection.

The way we display data in cellForRowAtIndexPath becomes a little different as now we have to consider different sections. The NSIndexPath object has a property called section and we can use this property to write the appropriate value to the text property of the cell. cellForRowAtIndexPath method will look like this


We also need to reload the data if teh same view is loaded again, as the second time the view may need to display different data. To do this, we implement the viewWillAppear method and pass the reloadData messaga to the tableView. This will make the tableview call the cellForRowAtIndexPath again. We also set the title of the view here and not in viewdidLoad method, so the title of the view reflects the coffee selected. This is how the method will look like

This concludes the second part of this tutorial. In the third part of the tutorial, you will learn how too /add/edit/delete the data.

You can download the source code by clicking here. Please do let me know if you found this article helpful or not and what I can I do in future.

I would also like to apologize because it is obvious by now, I did not have a plan on how I will write this tutorial and it has become quite complicated, I think.

 

17 Responses to iPhone SDK Tutorial – Using SQL Lite Part II

  1. iPhone SDK Articles says:

    >Hi Stenson,

    Yes, the third part of the tutorial is coming soon.

    Thank you for your support.

    Happy Programming,
    iPhone SDK Articles

  2. stenson says:

    >Just wondering if the next part of the tutorial which shows us how to add / delete entries is still coming? I tried to learn this stuff from other sources but your tutorials are MUCH easier to understand! I really appreciate what you doing. Thanks.

  3. Anonymous says:

    >I still do not understand how the setACoffeeHouse method is called.

    How did you provide your own set method implementation for the aCoffeeHouse property?

  4. Anonymous says:

    >Dear Jai,

    your tutorial really made light in my darkness. However I want to implemet a search in the coffeHouse list. Can you give a guideline how to make it? I somply can not drop a search bar into the views…

    Thanks:
    Peter

  5. jai says:

    >Hi Nando,

    You can refresh the RootViewController by calling reloadData like this
    [tableView reloadData]; Call this method in viewWillAppear method.

    So the final code will look something like this

    - (void)viewWillAppear:(BOOL)animated {
    [tableView reloadData];
    }

    Happy Programming,
    iPhoneSDKArticles

  6. Techjunkie says:

    >Wow! This is a really great tutorial and just what I needed. I’m an old C / C++ / Perl / Java programmer and I need some tips to learn the new stuff in CoCoa Touch! Thanks!!!!

  7. Anonymous says:

    >you’re hyperlinks, ‘a name’ #hrefs, in all your tutorials always point to the webkit one.

  8. Nando says:

    >It is a great tutorials..and am very glad that the site is available.

    I am very interested on performing sql on the iPhone and your tutorials are great!.

    I had been looking at Apples Book example..and created a small app that reads in the same fashion as your totorial and adds data.
    However I cant seem to refresh the “RootViewController” after using my “AddViewController”…so I would appreciate if you could implement the “Add” method in your code.
    I want to see how you get the RootViewController to do a refresh with the data.

  9. Lethal looks says:

    >hey, is there support for accessing a remote MySQL server for iphone applications!!! I am storing a lot of data which i’d need in my application. So i’m using a MySQL server… :(

  10. jai says:

    >Hi setACoffeeHouse is getting called by this line cvController.aCoffeeHouse = [appDeleg.coffeeHouseArray objectAtIndex:indexPath.row]; in didSelectRowAtIndexPath in RootViewController.m file.

    I have provided my own set method implementation for the aCoffeeHouse property. Which gets called by the above line.

    Thanks,
    iPhone SDK Articles
    http://www.iPhoneSDKArticles.com

  11. Anonymous says:

    >Jai, question concerning setACoffeeHouse, how does the rootview know to connect to that method I do not see any calls to it. Thanks

  12. boy_plunder says:

    >This is such a great help. I have been working on a database app for a few weeks now and these two sqlite examples have shown up the issues that were slowing me down. A great big thank you.

    The test app I am working on needs me to add a small image for each entry, would you consider doing an example on adding images? I’ll be damned if I can get it to work.

  13. Anonymous says:

    >it seems your site is very popular, and I can not download nor look at the images. Is there another location where I can get it from? Thanks

  14. Micha says:

    >hi!

    i wanted to download the source file but it doesn’t work. could you check the link, please?

    thanks!

  15. jai says:

    >Hello,

    Yes, I was indeed using a wrong method, I should have been using “stringByAppendingPathComponent” everywhere but I used “stringByAppendingString” by mistake. I guess I relied on Xcode code completion to read my mind.

    Good catch.

    Thanks,
    iPhone SDK Articles

  16. dingopup says:

    >I think that you may be missing a forward slash from your document path because SQLNavigation.sql appears in document’s parent directory. Also, ‘DocumentsSQLNavigation.sql’ is created in the same directory.

    Anyway, thanks for putting the effort in and creating this tutorial.