当调用相同的“DBName”值时,以下函数将返回不同的bool值。我期待找到一个重复项。第一次尝试有效,第二次尝试无效。我做错什么了吗?
-(BOOL)CheckForDB:(NSString *)dbPath{
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
if(checkForDB == nil){
const char *sql = "SELECT COUNT(*) FROM BookingInfo WHERE DBName = ?";
if(sqlite3_prepare_v2(database, sql, -1, &checkForDB, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error while fetching uploaded status. '%s'", sqlite3_errmsg(database));
}
}
sqlite3_bind_text(checkForDB, 1, [dbName UTF8String], -1, SQLITE_TRANSIENT);
NSLog(@"checking status %@",dbName);
while(sqlite3_step(checkForDB) == SQLITE_ROW) {
NSString *status=[NSString stringWithUTF8String:(char *)sqlite3_column_text(checkForDB, 0)];
return [status boolValue];
}
return NO;
sqlite3_reset(checkForDB);
}else{
sqlite3_close(database);
}
}
最佳答案
我找到了解决方案。我必须再调用sqlite3_reset(checkForDB)一次,然后再调用return YES
while(sqlite3_step(checkForDB) == SQLITE_ROW) {
NSString *status=[NSString stringWithUTF8String:(char *)sqlite3_column_text(checkForDB, 0)];
sqlite3_reset(checkForDB)
return [status boolValue];
}