如何在FMDB中选择行?

时间:2014-04-06 13:53:01

标签: ios fmdb

友 我使用FMDB并且它运行良好,但我尝试选择单行,但它给了我一个错误

这是代码

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

谢谢

2 个答案:

答案 0 :(得分:3)

您应该检查结果。例如:

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];

如果你不(a)检查executeQuery返回码,你就是盲目的; (b)如果失败,请检查lastErrorMessage,以便了解它为什么不起作用。

答案 1 :(得分:1)

你能试试吗?

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];