views:

105

answers:

3

This is my first SQLite based iPhone app and I am trying to get it to read a menu hierarchy from my database.

The database appears to be registered fine as the compiled statement doesnt error (tried putting in valid table name to test) but for some reason sqlite3_step(compiledStmt) doesnt ever equal SQLITE_ROW as if to suggest there is no data in there; which there is.

sqlite3 *database;

menu = [[NSMutableArray alloc] init]; 

if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    const char *sqlStmt = "SELECT * FROM Menu";
    sqlite3_stmt *compiledStmt;

    if (sqlite3_prepare_v2(database, sqlStmt, -1, &compiledStmt, NULL) == SQLITE_OK) {
        while (sqlite3_step(compiledStmt) == SQLITE_ROW) {
            NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStmt, 1)];

            MenuItem *menuItem = [[MenuItem alloc] init];
            menuItem.title = aTitle;

            [menu addObject:menuItem];

            [menuItem release];
        }
    }
    else {
        NSLog(@"There is an error with the SQL Statement");
    }

    sqlite3_finalize(compiledStmt);

}

sqlite3_close(database);
A: 

You should really be using Core Data. There is really no reason to use SQLLite directly now. Please take a look at the core data documentation:

Core Data

OscarMk
thanks but other people have made this comment, it isnt a solution to the question though
tigermain
A: 

In response to your comments:

Core data can be used for simple data or complex data. The setup is minimal and it's super easy to use. In terms of tutorials, apple's own documentation is the best. I'm a new iPhone developer myself and I was able to figure it out in a couple of hours. Apple has a great sample app called iPhoneCoreDataRecipes that helped me get up and running.

The other option is to look at plists. They can be used for storing simple data as well; typically configuration data. David Janes has written up a simple tutorial.

Gavin Miller
well Im talking about a data set of about 1000 records is Core Data still the way to go? Is it queryable like SQLite?
tigermain
Sorry I missed that part - Core Data is built on SQLite and 1000 records is not a problem at all.
Gavin Miller
That recipe demo looks really good, seems to be doing what I need, just develed into it more and it IS using a SQLite database, still think Im going to have the same problems though
tigermain
A: 

Despite most suggestions to use Core Data, I have spent the afternoon researching it and it is completely inappropriate to my needs. I need to store/import over 1000 rows of data in the application on load and Core Data has NO way to do this in any easy fashion.

SQLite will still be the ideal solution, but I still dont have a solution to the problem.

EDIT:

Further research led me to this AMAZING example:

http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html

With a little jiggary pockery you can import your CSV etc into a SQLite file for embedding in your application

tigermain