我无法从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!
答案 0 :(得分:6)
对于产生0行的查询,“result”永远不会为nil。
另外,您不应将对象指针与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" );
答案 1 :(得分: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);
}
如果没有条目,将设置等于零