How to select a line in FMDB?

Friends I used FMDB and it works very well, but I tried to select one line, but it gives me an error.

this is code

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

thank

+4
source share
2 answers

You must check your results. For instance:

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 flew blindly if you are not (a) checking the return code executeQuery; and (b) if this did not succeed, see lastErrorMessagethat you know why it did not work.

+3
source

Can you try this?

NSString *databasePath = [[NSBundle mainBundle] pathForResource:DBName ofType:@"sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:databasePath];

if (![db open]) {
    return;
}

FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:databasePath];

[queue inDatabase:^(FMDatabase *db) {

    FMResultSet *results = [db executeQuery:@"SELECT * FROM TableName"];

    while ([results next]) {

        NSString *str = [results stringForColumn:@"your_column_name"];
    }
    [results close];
}];

[db close];
+1
source

Source: https://habr.com/ru/post/1535247/


All Articles