2011-08-04 29 views
0

我想在我的sqlite數據庫中替換九個對象,但最後一個columne顯示兩次。我必須做什麼,只有我的九個物體被轉移,而不是另一個?最後colume在sqlite數據庫中顯示兩次

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 

回答

0

當時任一行添加,因爲它不存在於表中? SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE"

此外,你應該修改你的代碼是這樣的:

  1. 打開數據庫
  2. 如果okie,準備語句
  3. 如果okie,開始循環,將值綁定到準備好的語句。 sqlite3_reset()在每次迭代結束時的語句
  4. 完成循環,完成語句,關閉數據庫。
+0

非常感謝你:) – Lumus

+0

當你能夠這樣做時,請投我的答案:) –