Thursday, December 29, 2016

Android SQLite Database Tutorial (Create, Read, Update, Delete)

Sometime we need a database to support our development. In mobile, especially Android we using database SQLite.

SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.

In this tutorial i going to show you how to do CRUD (Create, Read, Update, Delete) on Android using Database SQLite.


There are 3 buttons action (Insert, Update and Delete) and 1 Listview to display the saved data. You can see as realtime.


Create some xml files that we in this project :
  1. activity_main.xml
  2. popup_delete.xml
  3. popup_update.xml
Create some java classes :
In this project i store all the string in strings.xml. Here it is :

    <string name="app_name">SQLLiteTutorial</string>
    <string name="input_your_name_bastard">Input your name, bastard</string>
    <string name="your_phone_number">your phone number</string>
    <string name="msg_cannot_allow_empty_field">Cannot allow empty field</string>
    <string name="lbl_update_data_pop_up">Update Data!</string>
    <string name="lbl_choose_an_id">Choose an ID</string>
    <string name="hint_update_phone_number">Update phone number</string>
    <string name="hint_update_name">Update name</string>
    <string name="lbl_save">Save</string>
    <string name="lbl_ok">OK</string>

1. Before we create the process in Java files, first modify all the xml file that we prepared before

Open activity_main.xml, paste the following code :

Open popup_delete.xml. This layout appears when button delete is pressed. Paste the following code :

Open popup_update.xml. This layout appears when button update is pressed. Paste the following code :

2. Next we modify all the java classes.

First create the model of your object. In this project i created as Paste the following code :

Take a look at method public String toString(). This method actually created to make us easy to display data on the spinner. You can modify the return value of this method related with your requirements.

Open This class will extends SQLiteOpenHelper, implement its methods. The methods that we implement after extends SQLiteOpenHelper are :
  1. (override)  onCreate(SQLiteDatabase sqLiteDatabase). This method  used to create the table of database
  2. (override)  onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1). This method used to increase database version after any changes in its attribute of name automatically. Beside that this method also check if there any duplicated name of table or not.
Beside create database name, also in this class we create all the main function of CRUD :

Create the data or Insert :

Read all the data that we have inserted. This method will return your list model (In this case : return profileList) :

Update the data that we have inserted using parameter ID (ID of the profile) :

Delete the data that we had using parameter ID (ID Profile) :
Those methods will do CRUD action in our database. Here the full code of :
After that, the process moves to In this class we do the technical process of CRUD. And also in every technical process will calling the methods from

When button insert is pressed, program will call method insertData(handler) :

After inserted and success, data will displayed on listview as realtime using method displayAllData(DatabaseHandler handler, ProfileModel profile) :

When button update is pressed, program will call method updateDataFromPopup() :

When the button update is pressed, there is a popup will appears. You have to choose ID that you will update, after that, fill or rewrite the value of the editText that you want to change.

Same as button update, when button delete pressed, there is a popup will appears. You just choose an ID that you want to delete. Here is the method popupDelete() :

Hope you understood how the process. And here is the full code of :

Done! Run your project and, wish it works well. Thank you.

Related Posts