Home Tutorials Hints & Tips

Getting Started in Android Development

Part 41 - Editing and Deleting Entries in the SQLite Database

In this tutorial we get to complete the work with our SQLite Database by creating methods to edit and delete a record.

When we wish to either delete or edit a record we need to identify the row ID of the record. In our case it is simply a case of viewing the database and then entering the number in the EditText field of the SqliteActivity’s layout. We would also need to enter some modified data in the EditText fields for the member’s name and their status. The code that retrieves these values is placed in the case statement for the “Edit Entry” button. This code is below:

memberName = nameText.getText().toString();
memberStatus = statusText.getText().toString();
rowId = Long.parseLong(rowIdText.getText().toString());

Note that we need to convert the string entry for the row ID to a long integer.

The next four lines of code create an object of the membersDatabase class, open the database, modify the entry and finally close the database.

MembersDatabase editMember = new MembersDatabase(this);
editMember.editEntry(rowId, memberName, memberStatus);

As we saw with the previous methods we created, Android Studio will generate an error if the editEntry() method has not been created in the membersDatabase class, but will create it for you if you if you wish. Note that in addition to the member’s name and status, we need to pass the row Id.

We can turn our attention to the editEntry() method. The code for this method is shown below:

public void editEntry(Long rowId, String memberName, String memberStatus) {
   ContentValues contentValues = new ContentValues();
   contentValues.put(KEY_NAME, memberName);
   contentValues.put(KEY_STATUS, memberStatus);
   memebersDatabase.update(DATABASE_TABLE, contentValues, KEY_ROWID + "=" + rowId, null);

We need to create an object of the ContentValues Class and then put the new name and status data into them. The last statement simply updates the database table with the new values for the defined row.

Deleting a record is straightforward. In the case statement for the “Delete Entry” button we will need to add the following code:

rowId = Long.parseLong(rowIdText.getText().toString());
MembersDatabase deleteMember = new MembersDatabase(this);

We only need to pass the row ID to a method we will create in the members database.

Only one line of code is needed to delete a record and the method is shown below:

public void deleteEntry(Long rowId) {
   memebersDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null);

In tutorials 37 to 41 we have looked at how to create a database, create records, view the database, edit records and delete records. The methods developed here can be used as a foundation for your future projects.

After this tutorial your MembersDatabase.java file should look similar to the one below:

Your SqliteActivity.java file should look similar to the one below:

Download Download tutorial set 2

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved