2011-05-10 63 views
0

我的應用程序中有一個設置選項卡,它通過來自用戶的edittext獲取一個int值並將其存儲到數據庫中。當用戶現在想要更改設置時,應該在數據庫中更新舊設置。我該如何編寫數據庫代碼。更新android數據庫中的值

下面是我的代碼保存在分貝值:

DatabaseHelper dha = new DatabaseHelper(this); 
      SQLiteDatabase db = dha.getWritableDatabase(); 
      Log.d("","Done with database Sqlite"); 
      ContentValues cv = new ContentValues(); 
      EditText editText2 = (EditText) this.findViewById(R.id.editText1); 
      setTime = editText2.getText().toString(); 
      Log.d(setTime, "This is the setTime value"); 
      cv.put(DatabaseHelper.TIME, setTime); 
      db.insert("finalP", DatabaseHelper.TIME, cv); 
      db.close(); 
+0

是的,我會......我的意思是我沒有嘗試過了低於建議。我處於某種事物的中間...... – 2011-05-10 17:35:41

回答

4

你應該做這樣的事情:

// Create content values that contains the name of the column you want to update and the value you want to assign to it 
ContentValues cv = new ContentValues(); 
cv.put("my_column", "5"); 

String where = "my_column=?"; // The where clause to identify which columns to update. 
String[] value = { "2" }; // The value for the where clause. 

// Update the database (all columns in TABLE_NAME where my_column has a value of 2 will be changed to 5) 
db.update(TABLE_NAME, cv, where, value);