DesperateLearner
DesperateLearner

Reputation: 1175

Sqlite returns different values - iOS

The below function when called for the same 'DBName' value returns different bool values. I was expecting a duplicate to be found. On first attempt it works and on second attempt it doesn't. Am I doing anything wrong?

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

Upvotes: 1

Views: 127

Answers (1)

DesperateLearner
DesperateLearner

Reputation: 1175

I have found the solution. I have to call sqlite3_reset(checkForDB) one more time before I call return YES

while(sqlite3_step(checkForDB) == SQLITE_ROW) {



    NSString *status=[NSString stringWithUTF8String:(char *)sqlite3_column_text(checkForDB, 0)];
    sqlite3_reset(checkForDB)
    return [status boolValue];
}

Upvotes: 1

Related Questions