2011-12-14 8 views
1

FMDBから空の結果セットをトラップする際に問題があります。 コードは次のとおりです。私はデータベースのオープンとクローズとNSLog "1"からNSLogを取得していますが、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"); 
    } 
} 

結果を返すデータベースとを、私はNO、YESので、私はhasAnotherRowはしばらく([結果の横])ループ内で行かなければならないと仮定結果2結果1を得ます。 空のデータベースでは、私は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

+0

これは期待通りに動作しません。上記の詳細。 ありがとう – Darren

+0

あなたは一度前に[result next]を呼び出して、それがnilを返すかどうかを確かめる必要があります。 – TomSwift

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

してもエントリーが

関連する問題