This is the fourth part of SQLite for Android series tutorials with examples. Here you can find the first part – SQLite for Android Part 3: Show Values from Database.

In this example is explained hot to update data previously inserted in to created database and table.

Layout

Add a edit text and button to the activity_main.xml layout:

Update method

Then we add updateData() method in our DataBaseHelper class:

Here we initiate local db object with our created database and update it with new values for designated row. We know what row to be updated by the id value that we receive as parameter in this method. ContentValues object is explained in the previous post. With .update method we are telling the database (STUDENT_TABLE), to add content value wher ID is equal to id that we receive in this method as a parameter. Every record (row) in the table have uniqe id value. ID = ? is weare clause which literally meas if ID = new String[](id) update that record. We surround this with try/catch because the update might be unsuccessful. and In that case we return false and tell the user that the record is NOT updated.

Update data implementation

ID field is added that we know exactly which row to be updated. If the update is successful we receive confirmation message.

 

You can get the whole project on GitHub /SQLiteDatabaseAndroid

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *