简体   繁体   中英

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) {

        // 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不是指针,因此%@是不正确的。

Use below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM