tech_learner
tech_learner

Reputation: 725

updating the values in android database

I have a settings tab in my application which takes an int value through edittext from user and stores it into the database. When the user now wants to change the setting the old setting should be updated in the database. How do I write the database code for that.

Below is my code to save the value in 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();

Upvotes: 0

Views: 5064

Answers (1)

TofferJ
TofferJ

Reputation: 4784

You should do something like this:

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

Upvotes: 4

Related Questions