开发者

Importing multiple records from sqlite db iphone sdk

开发者 https://www.devze.com 2022-12-21 11:12 出处:网络
I need some help! Please. I am trying to develop a very very simple application, basically just a simple book reader. Ideally, the first view would be a uitableview listing the names of chapters, then

I need some help! Please. I am trying to develop a very very simple application, basically just a simple book reader. Ideally, the first view would be a uitableview listing the names of chapters, then selecting the row would take you to a detail view that has the chapter which is broken up into segments. Note the segments are actually their own records inside of the table. So I am trying to populate a uitext view with multiple records from a db table. The process of getting all of my data from this db is where I'm just getting all messed up. I searched around on the internet for some tutorials and I found a very useful one here. I went through it step by step, and it made sense, it was all making sense. But instead of using the db he provided, I wanted to try inserting my own db, that has all of the content Id like to have viewable, and it wont work. Its throwing a nice *** Terminating app due to uncaught exc开发者_Go百科eption 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString' error that I can't figure out. Does that sound familiar or make sense to anyone? The code for mthe readFromDB method is:

-(void) readTextFromDatabase {
// Setup the database object
sqlite3 *database;

// Init the animals Array
textAr = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select * from books";//This is where I'm running into trouble
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aChapter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *aSection = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];


            // Create a new text object with the data from the database
            Text *text = [[Text alloc] initWithChapter:aChapter Section:aSection];

            // Add the text object to the text Array
            [textAr addObject:text];

            [text release];
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

Besides switching the names of some variables, I haven't changed any part of that code except for the place I mentioned is probably causing some problems. I changed the "select * from animals" statement to "select * from books" where animals was the name of the old table, and books is the name of the table in the new db I would like to use. I thought that wouldn't be too big of a deal, but apparently it is. I recognize that this is not a very tidy question, but if there is anyone out there that could help me over this wall I am facing, it would be greatly appreciated. Thanks!!!


I ran into the same issue when using sqlite, I can't exactly recall when the null strings were coming up (maybe from NULL values in the row), but I used this code to side-step the issue:

+ (NSString*)stringWithCharsIfNotNull: (char*)chars
{
    if ( chars == NULL )
        return nil;
    else
        return [[NSString stringWithUTF8String: chars] 
                stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
0

精彩评论

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

关注公众号