更新android数据库中的值

时间:2011-05-10 13:15:41

标签: android sqlite

我的应用程序中有一个设置选项卡,它通过来自用户的edittext获取int值并将其存储到数据库中。当用户现在想要更改设置时,应在数据库中更新旧设置。我该如何编写数据库代码。

以下是我在db:中保存值的代码:

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

1 个答案:

答案 0 :(得分: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);