2012-01-21 100 views
-1

可能重複:
update sql database with ContentValues and the update-method如何使用where子句在SQLite中進行更新查詢?

我使用此代碼,但我想更新它根據我的ID。

public void updateforgotpassword(String data) { 
     // TODO Auto-generated method stub 

     ContentValues forgotpass = new ContentValues(); 
     forgotpass.put("password", data); 

     try { 
      db.update(TABLE_NAME, forgotpass, null, null); 
     } 
     catch (Exception e) 
     { 
      String error = e.getMessage().toString(); 
     } 

    } 

我該怎麼做?

+0

你甚至嘗試尋找它呢? – JoxTraex

+0

參考這篇文章[http://stackoverflow.com/a/3874985/966550](http://stackoverflow.com/a/3874985/966550) – waqaslam

回答

2

假設該行的ID存儲在id變量和假設列名_id(這是標準的),你可以更新這樣

final String[] whereArgs = { Long.toString(id) }; 
db.update(TABLE_NAME, forgotpass, "_id = ?", whereArgs); 
1

您可以隨時使用rawquery方法中可以觸發純SQL查詢。

或者,也可以使用由SQLite和其中提供的「更新」方法提供兩個陣列,一個用於「whereargs」和一個是「wherevalue」

使用

"update [your table] set [your column]=value"在rowQuery方法.. 。

,或者

db.update(TABLE_NAME, forgotpass, "KEY_ID = ?", whereArgs); 
0
 public boolean updateAccountData(AccountModel account) { 

      boolean updated = false; 
      openDataBase(); 
      try { 
       final ContentValues values = new ContentValues(); 

       values.put("AccountName", account.getAccountName()); 
       values.put("Lessee", account.getLessee()); 
       values.put("BookingRedeliveryNumber", account 
         .getBookingRedeliveryNumber()); 
       values.put("SurveyorName", account.getSurveyorName()); 
       values.put("ManufacturerName", account.getManufacturerName()); 
       values.put("State", account.getState());    
       values.put("TurnInMonth", account.getTurnInMonth()); 
       values.put("TurnInYear", account.getTurnInYear()); 
       values.put("CscMonth", account.getCscMonth()); 
       values.put("CscYear", account.getCscYear()); 

       myDataBase.update(ACCOUNT_MST, values, "AccountID = " 
         + account.getAccountID(), null); 
       updated = true; 
    } 
return updated; 
    } 
相關問題