SQLite data showing SQLite data showing sqlite sqlite

SQLite data showing


If I understand correctly your problem is not so much about SQLite but much more about how to wire up your view controllers correctly.

When the user selects one of the days in your first UITableViewController you have to pass the information about the selection on to the next view controller. Assuming you are not using storyboards it would probably look something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:NO]; DaycusViewController *viewController = [[DaycusViewController alloc] initWithStyle:UITableViewStylePlain]; viewController.selectedDay = [days objectAtIndex:indexPath.row]; [[self navigationController] pushViewController:viewController animated:YES]; [viewController release];} 

As you now have the information available which coffees you want to display (e.g. the ones for "Monday") in your second view controller you can do for example what Diego suggested and filter your data accordingly.

Your current approach does probably not work because the UITableViewDataSource methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

and

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

depend on each other. So you have to be consistent here. The first one (...cellForRowAtIndexPath...) will be called for each row you want to display as you indicated in the second one (... numberOfRowsInSection ...).

So if you said you had 4 rows to display it will be called four times. What you cannot do is then use an if statement in the ...cellForRowAtIndexPath ... method to just return less than the four rows. Instead you should have a collection of the coffees for that specific day (ideally coming out of your data model ...). You can use the size of the collection for the ...numberOfRowsInSection... method and then return the corresponding element for each row using the index path in the ...cellForRowAtIndexPath ... method like this:

Coffee c = [coffees objectAtIndex: indexPath.row]cell.name.text = c.name;

There are some quite good examples in Apple's documentation and some sample code that covers exactly your problem as well.


It's hard to understand your question but i think you can better open a new project and start with Core Data. It's easy to understand and it's faster than SQLite.

Core Data is a framework Apple provides to developers that is described as a “schema-driven object graph management and persistence framework.” What does that actually mean? The framework manages where data is stored, how it is stored, data caching, and memory management. It was ported to the iPhone from Mac OS X with the 3.0 iPhone SDK release.

The Core Data API allows developers to create and use a relational database, perform record validation, and perform queries using SQL-less conditions. It essentially allows you to interact with SQLite in Objective-C and not have to worry about connections or managing the database schema

More about Core Data:

I wish you all the luck with your application, but i'm for sure that Core Data is the best for your application!


I'm by no means an expert of objective-c, but could this work?

// Pass dayoftheweek to the function(void) getInitialDataToDisplay:(NSString *)dbPath:(NSString *)dayoftheweek {    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {    const char *sql = "select coffeeID, coffeeName from coffee where dayoftheweek = ?";    sqlite3_stmt *selectstmt;    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {        // Pass dayoftheweek to the query        sqlite3_bind_text(addStmt, 1, [dayoftheweek UTF8String], -1, SQLITE_TRANSIENT);        // Rest of the code, now the query will only return the data for the specified day