2014-02-06 61 views
2

我一直在閱讀sqlite更新查詢的所有答案,但我的工作不正常。iOS上的Sqlite數據庫查詢只更新一個字段

它只更新「categoria」列,但不是「用戶評論」

「categoria」是我的SQLite數據庫的最後一列,但我有一個,即時通訊沒有更新很多列,這會影響我查詢?我應該更新每一列,雖然我沒有更新它們嗎?

這裏是我的代碼:

NSString *query = [NSString stringWithFormat:@"UPDATE apps set userComment='%@', categoria='%@' WHERE id='%d'",app.userComment, app.categoria, [[app appID] intValue]]; 

    sqlite3_stmt *statement; 

    if (sqlite3_prepare_v2(_dataBase, [query UTF8String], -1, &statement, nil) 
     == SQLITE_OK) 
    { 

     NSLog(@"error: %s", sqlite3_errmsg(_dataBase)); 

     if (sqlite3_step(statement) != SQLITE_DONE) 
     { 
      NSLog(@"error: %s", sqlite3_errmsg(_dataBase)); 
     } 
     else 
     { 
      NSLog(@"updateContact SUCCESS - executed command %@",query); 
     } 
     sqlite3_reset(statement); 
     sqlite3_step(statement); 
     sqlite3_finalize(statement); 

      // sqlite3_finalize(statement); 
    } 
    else 
     NSLog(@"error: %s", sqlite3_errmsg(_dataBase)); 

我查詢的NSLog的似乎是正確的,而我可不是得到任何錯誤! 我不知道發生了什麼事.. 請幫忙! 謝謝!

+0

你知道嗎? – bryanmac

+0

使用sqlite3命令行工具來轉儲數據庫。 「apps」表是否包含「userComment」列? –

+0

@bryanmac非常感謝,問題出在SELECT和INSERT方法上,我對綁定列感到困惑,我將你的答案標記爲正確,非常感謝。 – Pach

回答

0

你把引號內的一個int值:(?)

WHERE id='%d' ... [[app appID] intValue] 

使用PARAMS並綁定一個int/Int64類型,以避免:

sqlite3_bind_int(statement, 3, [[app appID] intValue]); 

在你sqlite3_step電話,確保你檢查檢查完成。

if (sqlite3_step(statement) != SQLITE_DONE) 
{ 
    NSLog(@"error: %@", sqlite3_errmsg(_dataBase)); 
} 

NSLog退出您的更新聲明。確保它實際上具有您認爲應該的userComment值。

使用參數?對於價值和使用綁定和使用UTF8字符串:

sqlite3_bind_text(statement, 1, [[app userComment] UTF8String], -1, SQLITE_STATIC); 

由於sqlite3的是一個C API,你應該使用NULL,而不是零在你準備的發言。

打印我們的sqlite3_errmsg後,確定準備調用是不需要的。重置後檢查更有趣,步驟。

作爲參考,看看在這個答案我的代碼:

how to save the data in sqlite3 in iphone

自檢已更新的代碼,我知道的作品。

2

嘗試參數。

- (void) update 
{ 
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
    { 
     if(updateStatement == nil) 
     { 
      const char *sql = "UPDATE apps set userComment = ?, categoria = ? WHERE id = ?"; 
      if(sqlite3_prepare_v2(database, sql, -1, & updateStatement, NULL) != SQLITE_OK) 
       NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database)); 
     } 

     sqlite3_bind_text(updateStatement, 1, [comment UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(updateStatement, 2, [categoria UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_int(updateStatement, 3, id); 


     if(SQLITE_DONE != sqlite3_step(updateStatement)) 
      NSAssert1(0, @"Error while updating data. '%s'", sqlite3_errmsg(database)); 
     else 
      //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid 
      //noteID = sqlite3_last_insert_rowid(database); 

      //Reset the update statement. 
      sqlite3_reset(updateStatement); 
     sqlite3_close(database); 
     deleteStatement = nil; 
    } 
    else 
     sqlite3_close(database); 
} 
+0

This works too too,I just seen the previous answer firts,Thanks to lot ..! – Pach

0

我有簡單的解決方案來處理更新查詢。現在你不需要使用sqlite3_bind_text或sqlite3_bind_int。

只需使用更新查詢正常查詢和傳遞參數查詢,我們使用「%@」

在這裏,在字符串傳遞是簡單的查詢

queryString = [NSString stringWithFormat:@"UPDATE <Table-name> SET <column-name> = '%@', <column-name2> = '%@';", value1, value2]; 

if (sqlite3_prepare(contactDB, query, -1, &statement, NULL) == SQLITE_OK) 
{ 

} 

if (sqlite3_step(statement) == SQLITE_DONE) 
{ 
    //NSLog(@"ok"); 
} 
sqlite3_finalize(statement); 

使用上面的代碼,你將能夠後運行更新查詢。希望它能幫助別人。

+0

WHERE部分呢? – iphondroid

+0

以上查詢是使用Update查詢的一個例子,我們可以根據我們的要求更改查詢。 –