[英]I can't trap FMDB empty resultset
我無法從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否,result2是,因此我假設hasAnotherRow必須進入while([result next])循環內。 但是對於一個空的數據庫,我得到result1否,甚至沒有得到result2!
對於產生0行的查詢,“結果”永遠不會為零。
同樣,您不應該將對象指針與NULL
比較-與nil
比較。 看到這個問題: Objective-C中的NULL vs nil
嘗試這個:
FMResultSet *result = [[self getDatabase]executeQuery:@"SELECT BFNeeded FROM tblBets ORDER BY pk DESC LIMIT 1,1;"];
NSLog ( @"has results: %@", [result hasAnotherRow] ? @"YES" : @"NO" );
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);
}
如果沒有條目,將計數設置為零
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.