Search code examples
iosfmdb

How to select row in FMDB?


Friends I Used FMDB and it is working very well, but i tryed to select single row but it is gives me an error

this is code

FMResultSet *fResult= [db executeQuery:@"SELECT * FROM contents WHERE id = 1"];
[fResult next];
NSLog(@"%@",[fResult stringForColumn:@"title"]);

Thank you


Solution

  • You should check your results. For example:

    FMResultSet *rs = [db executeQuery:@"SELECT * FROM contents WHERE id = 1"];
    
    if (!rs) {
        NSLog(@"%s: executeQuery failed: %@", __FUNCTION__, [db lastErrorMessage]);
        return;
    }
    
    if ([rs next]) {
        NSString *title = [rs stringForColumn:@"title"];
        NSLog(@"title = %@", title);
    } else {
        NSLog(@"%s: No record found", __FUNCTION__);
    }
    
    [rs close];
    

    You are flying blind if you don't (a) check the executeQuery return code; and (b) if it failed, examine lastErrorMessage so you know why it didn't work.