2011-12-14 150 views
1

我无法捕获FMDB中的空结果集。 代码如下。我从数据库打开和关闭NSLog和NSLog“1”,但没有在If语句中的! 如果我有数据库中的数据很好,但我想陷阱和编辑结果,如果数据库是空的。我无法捕获FMDB空结果集

[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,YES RESULT2所以我承担hasAnotherRow必须要走的一段时间([结果旁边])循环中。 但是,对于空数据库,我得到了result1 NO,它甚至没有得到result2!

回答

4

对于产生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"); 
+0

非常感谢。我不知道hasAnotherRow。 – Darren 2011-12-15 13:02:02

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); 
} 

将设置数等于零,如果没有该项