0

I wanted to replace nine objects in my sqlite database but the last columne shows up twice. What do I have to do that only my nine objects get transferred and not an additional one?

for (NSInteger i = 0; i <9; i++) {
    NSString * checkNSString = [checklistLevel objectAtIndex:i];

    NSString * SkillLevelNSString = [SkillLevel objectAtIndex:i];

    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {   



        char *errorMsg;
        char *replace = "REPLACE Into SkillLevel4 (rowid, CheckboxSkillLevel4, SkillLevel4) Values (?,?,?);";

        sqlite3_stmt *stmt;
        if (sqlite3_prepare_v2(database, replace , -1, &stmt, nil)
            == SQLITE_OK) {
            sqlite3_bind_int(stmt, 1, i);   
            sqlite3_bind_text(stmt, 2, [checkNSString UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 3, [SkillLevelNSString UTF8String], -1, SQLITE_TRANSIENT);

            if (sqlite3_step(stmt) != SQLITE_DONE)
                NSAssert1(0, @"Error updating table: %s", errorMsg);
            sqlite3_finalize(stmt);
        }   
        }
        sqlite3_close(database);


}

Database:

  "0"   "1" "demonstrate 4 types of mounts"
  "1"   "1" "ride backward for 10 meters"
  "2"   "0" "ride one footed for 10 meters"
  "3"   "0" "idle with left foot down 25 times"
  "4"   "0" "idle with right foot down 25 times"
  "5"   "1" "ride with seat out in front for 10 meters"
  "6"   "1" "ride with the seat out in back for 10 meters"
  "7"   "1" "make a 360 degree turn to the left inside a 1 meter circle"
  "8"   "0" "make a 360 degree turn to the right inside a 1 meter circle "
  "9"   "0" "make a 360 degree turn to the right inside a 1 meter circle " <-twice
Lumus
  • 1

1 Answers1

1

Was any row added because it did not exist in the table? SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE".

Moreover, you should modify your code like this:

  1. Open the database
  2. If okie, prepare the statement
  3. If okie, start loop, bind values to prepared statement. sqlite3_reset() the statement at the end of every iteration
  4. Finish loop, finalize the statement, close the database.
Community
  • 1
  • 1
Daniel Le
  • 1,800
  • 1
  • 14
  • 23