2011-12-14 115 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); 
} 

將設置數等於零,如果沒有該項