最后一列在sqlite数据库中显示两次

时间:2011-08-04 12:10:05

标签: objective-c xcode sqlite

我想在我的sqlite数据库中替换九个对象,但最后一个columne显示两次。我该怎么做只有我的九个物品被转移而不是另外一个?

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


}

数据库:

  "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

1 个答案:

答案 0 :(得分:1)

是否添加了任何行,因为表中不存在? SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE"

此外,你应该修改你的代码:

  1. 打开数据库
  2. 如果确定,请准备声明
  3. 如果确定,请启动循环,将值绑定到预准备语句。 sqlite3_reset()每次迭代结束时的语句
  4. 完成循环,完成语句,关闭数据库。