开发者

Why does SQLite not bring back any results from my database

开发者 https://www.devze.com 2022-12-18 04:44 出处:网络
This is my first SQLite based iPhone app and I am trying to get it to read a menu hierarchy from my database.

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开发者_如何学C");
    }

    sqlite3_finalize(compiledStmt);

}

sqlite3_close(database);


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消