North Border Software

Home Tutorials Hints & Tips About
Share on Facebook Share on Twitter

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 file should look similar to the one below:

Your file should look similar to the one below:

Download Download tutorial set 2

© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo

Android Apps and Training