2

我无法从 FMDB 捕获空结果集。代码如下。我从数据库打开和关闭以及 NSLog "1" 中获取 NSLog,但在 If 语句中没有一个!如果我在数据库中有数据很好,但如果数据库为空,我想捕获和编辑结果。

    [self openDatabase];

NSNumberFormatter *nfcurrency = [[NSNumberFormatter alloc]init];
[nfcurrency setNumberStyle:NSNumberFormatterCurrencyStyle];
[nfcurrency setLocale:[NSLocale currentLocale]];

FMResultSet *result = [[self getDatabase]executeQuery:@"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
//FMResultSet *result = [[self getDatabase]executeQuery:@"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
NSLog(@"1");
if (result == NULL) {
    NSLog(@"Last BFNeeded Result = nil");
} else {
    while ([result next]) {
        NSLog(@"HERE");
        NSString *lastBFNeeded = [nfcurrency stringFromNumber:[NSNumber numberWithDouble:[result doubleForColumn:@"BFNeeded"]]];
        NSLog(@"lastBFNeeded=%@",lastBFNeeded);
    }
}

NSLog(@"ClosingDB");
[self closeDatabase];

收到第一个回复后继续:

我无法让 hasAnotherRow 按预期工作。我有这个代码:

FMResultSet *result = [[self getDatabase]executeQuery:@"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 0,1;"];

if (result == nil) {
    NSLog(@"Last BFNeeded Result = nil");
}
else {
    NSLog(@"has results1: %@", [result hasAnotherRow] ? @"YES" : @"NO");
    while ([result next]) {
        NSLog(@"has results2: %@", [result hasAnotherRow] ? @"YES" : @"NO");
    }
}

对于返回结果的数据库,我得到 result1 NO, result2 YES 所以我假设 hasAnotherRow 必须进入 while ([result next]) 循环。但是,对于一个空数据库,我得到了 result1 NO,它甚至没有得到 result2!

4

2 回答 2

6

对于产生 0 行的查询,“结果”永远不会为零。

此外,您不应将对象指针与NULL-- 与nil. 看到这个问题:NULL vs nil in Objective-C

试试这个:

FMResultSet *result = [[self getDatabase]executeQuery:@"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];

NSLog ( @"has results: %@", [result hasAnotherRow] ? @"YES" : @"NO" );
于 2011-12-14T21:22:24.617 回答
0
NSInteger count;

query=[NSString stringWithFormat:@"select Count(*) from %@  where name = 'brandon' ",dbName];
results = [database executeQuery:query ];
while([results next]) {
    count  = [results intForColumnIndex:0];
    NSLog(@"count:%d",count);
}

如果没有条目,将设置计数为零

于 2015-05-29T00:58:05.810 回答