开发者

BAD_ACCESS on SQLITE selection

I am getting EXC_BAD_ACCESS when I attempt to do anything with the value I'm selecting from the local SQLITE database on an iPhone development. The code for my function is

-(void) updateFromDB {

// Setup the database object
sqlite3 *database;

// Open DB
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    NSString *query = [NSString stringWithFormat:@"SELECT MOD_ID FROM MODULE;"];

    //NSLog(@"QUERY: %@",query);

    // Prepare statement
    sqlite3_stmt *statement;
    if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, NULL) == SQLITE_OK) {

        //开发者_C百科 Execute SQL
        while (sqlite3_step(statement) == SQLITE_ROW) {

            // Get MOD_IDs
            NSInteger MOD_ID = sqlite3_column_int(statement, 0);

            NSString *ID = [NSString stringWithFormat:@"%@",MOD_ID];

            //=======================
            // Get Notice Module Data
            //=======================
            if (MOD_ID == 1) {
                self.noticeModule = [[ModuleSetting alloc] initWithID:ID];
            }




        }

    } else {
        NSAssert1(0,@"Error: failed to prepare statement. '%s'", sqlite3_errmsg(database));
    }

    // Release the compiled statement from memory
    sqlite3_finalize(statement);

} else {
    sqlite3_close(database);
    NSAssert1(0,@"Failed to open database. '%s'",sqlite3_errmsg(database));
}
sqlite3_close(database);

}

The bad access occurs on the line

NSString *ID = [NSString stringWithFormat:@"%@",MOD_ID];

Thanks for any help you can offer!


%@ denotes objects. But MOD_ID seems to be an integer. So your format should be %d,

NSString *ID = [NSString stringWithFormat:@"%d", MOD_ID];


You can't use %@ in format strings for integers, only for Obj-C objects. For integers, use %d (or for NSInteger, I think it is recommended to use %ld).

Have a look at the String Format Specifiers guide.


MOD_ID is not a pointer, so %@ isn't correct.


Use below

 NSInteger MOD_ID = sqlite3_column_int(statement, 0);
   NSString *ID = [NSString stringWithFormat:@"%d",MOD_ID];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜