This tutorial is about SQLite databases in Android. Here, we will perform C.R.U.D. operations by creating our own DatabaseHelper class. This Android SQLite database example application will store the student information ID, name, and phone number inside our SQLite table.

What is SQLite in Android? 

SQLite is an open source relational database that stores data to a text file on an Android device. Normal SQL (Structured Query Language) can be used to access a SQLite database in Android. Each application on a device can have a database associated with it.

In this Android SQLite example, we are going to store a list of students inside our database. Here you will learn to perform all C.R.U.D. operations (Create, Read, Update and Delete operations) using a SQLite database in Android.

For adding the SQLite database connectivity in Android, we need to create a separate class for the database. Here, we will name it DatabaseHelper. Now this DatabaseHelper class must implement the SQLiteOpenHelper which provides various methods to interact with the database.
Snippet 1 :


The DatabaseHelper Class contains 2 very important methods; onCreate and onUpgrade.

  1. onCreate() – This method gets called when the database is created for the first time. Tables would be created inside this method.
    Snippet 2 :
  2. onUpgrade() -  This method allows you to upgrade your database. You can update an existing database, drop existing tables, and recreate it via the onCreate() method.
    Snippet 3 :


Let’s learn about C.R.U.D. operations for our Android SQLite database application. Inside our DatabaseHelper class, we will be adding methods for C.R.U.D. operations.

Insert operation – For inserting values to the database, we need to save data into contentvalues. ContentValues allow defining key/value pairs. The key represents the table column name and the value represents the content corresponding to that column.
Snippet 4 :

Update Operation: To update the existing record in your Android sqlite database table, you need to execute the following method:
Snippet 5 :

  1. The first argument takes the name of the table.
  2. The second argument takes a new record.  Here, we pass the contentvalue object which contains data corresponding to each column in a key/value pair. So, all the columns which need to get updated are provided in the contentvalue object as a key/value pair.
  3. The third argument specifies the where clause (it’s a condition). For example, update the employee name where emloyee ID is “123”.  Here, the where clause is the “id=”.
  4. The fourth argument takes the value corresponding to the where clause. For the above example it is “123”.

Delete Operation: Similarly, you can perform the delete operation on an Android SQLite database table. It takes three arguments. The first is the table name, the second is the where clause, and the third is the value corresponding to that where clause.
Snippet 6 :

Read Operation: To read values from an Android SQLite database table, we need to learn about cursors. We execute the select operation on the database and we get multiple rows as a result. We assign those rows to a cursor. A Cursor points to one row at a time. This is how we get the required data.

Raw Query: This query directly accepts SQL as an input. You can pass SQL statement(s) and SQLiteDatabase will execute that query for you.
Snippet 7 :

Now coming back to the cursor, we assign the result of the select statement to the cursor and manipulate the data.
Snippet 8 :

You can use a for loop to get all the results. The moveToNext() method, as the name implies, helps to do so.

Now let’s implement the DatabaseHelper class. We are using the StudentsModel class as a model for the Android SQLite table.

Create a new file
Listing 1 :

Create a new file
Listing 2 : 

So, the database part is complete. We can do the connection part of the database with other classes.

  1. We will use an ArrayList to store students when we get data from a table using the cursor.
  2. We will create the DatabaseHelper object and perform SQLite database C.R.U.D. operations with the help of it.
  3. The print() method is used to display data on a textview. It will get all the results from a table by using the getAllStudentsList() method of the DatabaseHelper class.
  4. We have two buttons on our main screen. The Add button is for inserting a new student inside the Android SQLite database table. By specifying the student ID, we can delete it from records by clicking the Delete button.
  5. The addStudentDetail() method is for adding a single student to the Android SQLite database table.


Create a new file
Listing 3 :

The XML file.

Create a new file activity_main.xml
Listing 4 :

This was all about a simple Android SQLite database connection tutorial. If you have any suggestions or got any errors while implementing this procedure, feel free to comment in the section below.