Android: Storing Data Using SQLite Database

There are different ways to store data in Android. One of them is storing in the database. The most commonly used database in Android is SQLite. In this tutorial, I will demonstrate how to use SQLite in Android application for storing, retrieving, modifying and deleting data.

We will be using movie database with the following structure for this tutorial:

Movies Table Structure

Movie Table Structure

Creating the Project

First of all lets create a project in Android Studio and name it SQLiteTutorial and give package name as com.androiddeft.sqlitetutorial. You can check detailed steps on how to create a project in Android Studio here: Installing Android Studio and Running your First Android Application

Defining Movies Class

Let’s define a java class which will be useful in holding movie properties during different database operations. Define getters and setters as well. Create package com.androiddeft.sqlitetutorial.vo under the main package. Now create a class named MovieDetailsVO (Right Click on package ==> New ==> Java Class) inside newly created package. Define the 4 properties of movies database. To generate getters and setters, Right Click ► Generate ►Getters and Setters ► Choose properties for which you wish to generate getters and setters.


Defining SQLite Databse Handler

We have to create a database helper which will help us to perform CRUD (Create-Retrieve-Update-Delete) operations. First of all, create a package named com.androiddeft.sqlitetutorial.helper inside the main package. Now create a class named SQLiteDBHelper which extends the class SQLiteOpenHelper provided by Android. After creating the class you need to extend the methods

onCreate() : Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

onUpgrade() : Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

All CRUD Operations (Insert, Update,Select,Delete)

We will declare 5 methods to accomplish all CURD operations:

Inserting a new Record

We populate the object of the class MovieDetailsVO with movie details and call addMovie(MovieDetailsVO) method of SQLiteDBHelper class. The addMovie() method implements the insert operation. In order to establish the database connection and write to the database, we need to call getWritableDatabase() method of the parent class. It is always recommended to close the database connection after performing the desired operation.

Selecting a single Record

In order to select a single row from the table, we need to pass the movieId as the parameter to getAllMovies() method.Here movieId is the primary key of the movies table. Here we need to require to call getReadableDatabase() method of the parent class in order to initialize the database connection. The method returns MovieDetailsVO value object, which has the details of the movie.

Retrieving all Records

To read all the rows from the database we need to fire a raw select query and then iterate over the cursor and populate a list of MovieDetailsVO object.

Updating a Record

When you wish to update the details of the movie, you can pass the MovieDetailsVO object with the updated details to updateMovie(MovieDetailsVO) method.

Deleting a Record

For deleting a movie record you need to pass movieId to the deleteMovie(int) method.

Complete class

The User Interface

In order to make use of the application, we need a user interface. We will implement the simplest UI for this application and the xml code for the same goes below.


The below java code instantiates the SQLiteDBHelper object and uses its method to perform all CRUD operations

You can see the below video on how to use the app. If you have any queries you can write them down in the comments section. I will try my best to provide a solution.

Download Source CodeDownload APK

Stay Connected

No spam guarantee.



Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.

2 thoughts on “Android: Storing Data Using SQLite Database”

Leave a Reply

Your email address will not be published. Required fields are marked *