How to Update and Delete data from SQLite database in Android

Hello Android friends… This is going to be my first blog post for the Dooms Year (2012) 😉 By the way, I have to keep a promise although I wanted to write a new topic. Therefore I am continuing with the SQLite post series and today I am going to show you how to do Update and Delete functions. 

In my previous posts I have shown you Insert and Retrieve functions. So the interfaces are not going to change a lot except for new Buttons for Update, Delete and going back to previous interface.

According to my procedure, to update an item you have to click on that particular item from the list. Then you will be redirect to an interface where you can edit details of that particular item that you have clicked. First figure shows you the story till that moment.

Update and Delete data from SQLite database in Android
Figure 1

After you edit the values in the EditText fields, click on the Update button. Then you will redirect to the first interface in our simple program where we show the Undergraduates’ names. If you have updated the Undergraduate name, then you will be able to see the changes in the ListView also. If not click on the same item and you can see the updated details as in the following figure.

Update and Delete data from SQLite database in Android
Figure 2

By pressing the Cancel button you can go to the previous interface where the Undergraduates’ names list visible.

Deleting is also the same. First you click on the item you want to delete. Then it will redirect you to the interface where you can see all the details of the clicked item. Then by pressing the Delete button you can delete the selected item with all it’s details.

All above I have described about the front end. Here is the back end code with detailed comments. Please copy the source in to an IDE where you can clearly differentiate comments from the code and read the comments to get the idea of that particular code sample.

package com.anuja.sqlite;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class UpdateDeleteUndergraduateActivity extends Activity implements OnClickListener {

	private EditText uGraduateNameEditText;
	private EditText uGraduateUniIdEditText;
	private EditText uGraduateGpaEditText;
	private Button cancelButton;
	private Button updateButton;
	private Button deleteButton;
	private String bundledUgraduateName;
	private String bundledUgraduateUniId;
	private String bundledUgraduateGpa;
	private Double bundleUgraduateGpaDouble;
	private String ugraduateNameEditTextValue;
	private String ugraduateUniIdEditTextValue;
	private Double ugraduateGpaEditTextValue;

	protected void onCreate(Bundle savedInstanceState) {

		uGraduateNameEditText = (EditText) findViewById(;
		uGraduateUniIdEditText = (EditText) findViewById(;
		uGraduateGpaEditText = (EditText) findViewById(;

		cancelButton = (Button) findViewById(;
		updateButton = (Button) findViewById(;
		deleteButton = (Button) findViewById(;

		Bundle takeBundledData = getIntent().getExtras();

		// First we need to get the bundle data that pass from the UndergraduateListActivity
		bundledUgraduateName = takeBundledData.getString("clickedUgraduateName");
		bundledUgraduateUniId = takeBundledData.getString("clickedUgraduateUniId");
		// setText method ask for a String value
		//But getDouble method returns a Double value
		bundleUgraduateGpaDouble = takeBundledData.getDouble("clickedUgraduateGpa");
		//So we need to convert that Double value to String value
		bundledUgraduateGpa = Double.toString(bundleUgraduateGpaDouble);

		// Set the values that we extracted from the Bundle in the EditText fields

	public void onClick(View v) {

		// We need to update or delete details which is in the EditText fields after user edit the values
		// These values are the ContentValues that we are going to use in future
		ugraduateNameEditTextValue = uGraduateNameEditText.getText().toString();
		ugraduateUniIdEditTextValue = uGraduateUniIdEditText.getText().toString();

		String ugraduateGpaEditTextValueStr = uGraduateGpaEditText.getText().toString();
		ugraduateGpaEditTextValue = Double.valueOf(ugraduateGpaEditTextValueStr);

		// It is easy to set values to the POJO class and pass the class instance to the updateUgraduateDetails() method
		UndergraduateDetailsPojo undergraduateDetailsPojo = new UndergraduateDetailsPojo();


		// POJO class ask for a double value for the GPA
		//So we have to convert the EditText value to Double value
		Double ugraduateGpaDoubleValue = Double.parseDouble(bundledUgraduateGpa);

		if(v.getId() =={
		}else if(v.getId() =={
		}else if(v.getId() =={

	private void updateUgraduateDetails(UndergraduateDetailsPojo undergraduateDetailsPojo) {

		AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
		SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();

		// ContentValues class is used to store a set of values
		//It is like name-value pairs
		// "value" part contains the values that we are going to UPDATE
		ContentValues contentValues = new ContentValues();
		contentValues.put(AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_NAME, ugraduateNameEditTextValue);
		contentValues.put(AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_UNI_ID, ugraduateUniIdEditTextValue);
		contentValues.put(AndroidOpenDbHelper.COLLUMN_NAME_UNDERGRADUATE_GPA, ugraduateGpaEditTextValue);

		// If we are using multiple whereClauseArguments, array size should have to change
		String[] whereClauseArgument = new String[1];
		whereClauseArgument[0] = undergraduateDetailsPojo.getuGraduateName();

		System.out.println("whereClauseArgument[0] is :" + whereClauseArgument[0]);

		 * This is the normal SQL query for UPDATE
		UPDATE table_name
		SET column1=value, column2=value2,...
		WHERE some_column=some_value

		sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_GPA, contentValues, AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_NAME+"=?", whereClauseArgument);
		// For two whereClauseArguments
		//sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_GPA, contentValues, BaseColumns._ID+"=? AND name=?", whereClauseArgument);


	private void deleteUgraduateDetails(UndergraduateDetailsPojo deleteUndergraduateDetailsPojo) {

		AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
		SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();

		String[] whereClauseArgument = new String[1];
		whereClauseArgument[0] = deleteUndergraduateDetailsPojo.getuGraduateName();

		// Only difference between UPDATE and DELETE is
		//DELETE does not have ContentValues part
		sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_GPA, AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_NAME+"=?", whereClauseArgument);


That is the end of this post. As I have promised to you all here is the link to download source from the GitHub.

Thank you for staying with me 🙂

About AnujAroshA

Working as an Associate Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in Android Examples and tagged , , , . Bookmark the permalink.

31 Responses to How to Update and Delete data from SQLite database in Android

  1. Girish says:

    please upload the full source code.

  2. naila says:

    can you please provide a full source code m gettign errors 😦 plzzzzz plz plz plz

    • anujarosha says:


      I have uploaded the source to GitHub and I have also provided the link in my post. Please see the lase paragraph of the post.

      Good luck…!!!

      • dhanaraj says:

        could you please provide the XML file or upload it in the same link in GITHUB , that will be of great help for me. Thanks in advance.

  3. Aqeela says:


    You have provided the java files but not the xml files.

  4. Sandeep Honey says:

    Thanks for such a awesome Explanation

  5. Raghavendra says:

    good evening anuja ……. i have used your code… data inserting is working …. but data retrieving is not working properly…. instead of names only one variable is appearing that we hav used in code i e graduate name . in update and delete case even its not moving to next page after clicking on item …. have you used simple list view or customised list view with row.xml …..pls pls pls guide me …. what is the possible problem

  6. Muralidharan says:

    Hey Anuj… Thanks for postin this tutorial.. I really appreciate your effort in compliling this tutorial.

  7. Poonam says:

    thanks anuja

  8. Niyamathulla says:

    can u send me the xml files to me, i m getting errors pls. My email

  9. Peter says:

    I wish you could post the entire project with the supporting xml files.

  10. rawr says:

    where are the xml files?

  11. simran says:

    I am getting errors while executing this project.. pls send me xml files for this project
    my email is:

  12. vinicius carvalho says:

    hi ,
    when i try to salve anything in database, i get errors. Can u send me de full source?


  13. Mahivardhan Singh says:

    hi…..Can u send me full source code…….

  14. manasa says:

    Please provide the XML files

  15. You sent full source code to me pls T_T

  16. 03-23 12:02:45.229: D/AndroidRuntime(333): Shutting down VM
    03-23 12:02:45.229: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    03-23 12:02:45.249: E/AndroidRuntime(333): FATAL EXCEPTION: main
    03-23 12:02:45.249: E/AndroidRuntime(333): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.tasksfull/com.example.tasksfull.AddNewUndergraduateActivity}; have you declared this activity in your AndroidManifest.xml?
    03-23 12:02:45.249: E/AndroidRuntime(333): at
    03-23 12:02:45.249: E/AndroidRuntime(333): at
    03-23 12:02:45.249: E/AndroidRuntime(333): at
    03-23 12:02:45.249: E/AndroidRuntime(333): at
    03-23 12:02:45.249: E/AndroidRuntime(333): at com.example.tasksfull.UndergraduateListActivity.onClick(
    03-23 12:02:45.249: E/AndroidRuntime(333): at android.view.View.performClick(
    03-23 12:02:45.249: E/AndroidRuntime(333): at android.view.View$
    03-23 12:02:45.249: E/AndroidRuntime(333): at android.os.Handler.handleCallback(
    03-23 12:02:45.249: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(
    03-23 12:02:45.249: E/AndroidRuntime(333): at android.os.Looper.loop(
    03-23 12:02:45.249: E/AndroidRuntime(333): at
    03-23 12:02:45.249: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
    03-23 12:02:45.249: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(
    03-23 12:02:45.249: E/AndroidRuntime(333): at$
    03-23 12:02:45.249: E/AndroidRuntime(333): at
    03-23 12:02:45.249: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
    03-23 12:02:49.739: I/Process(333): Sending signal. PID: 333 SIG: 9

  17. majid says:

    would you please send me monifestfile and Xml file.thanks.

  18. ashwini says:

    would you please send me Xml files

  19. MrBrown says:

    would you please send me manifestfile and Xml file.thanks.

  20. leo murphy says:

    hey Anuja can you send me xml files and manifest just to compare to mine please
    email is

  21. Mudassarar says:

    hi…..Can u send me full source code


  22. Ravina says:

    please…send me full source code

  23. Jeet Trivedi says:

    not able to find GPA_Viewer Class in Android Manifest pl help me with asap!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s