Affiliate links on Android Authority may earn us a commission.Learn more.
How to use SQLite to store data for your Android app
August 07, 2025
Most Android apps will need to persist user data at sometime. There are different ways to store user data, but SQLite databases are a very convenient and speedy method of saving user (or app) data and information.
SQLite is an opensource SQL database that stores the database as a text file on a device. Basic familiarity with SQL sets up a developer nicely to use Android’s sqlite implementation. And for developers not familiar with SQL, do not be discouraged, SQL is pretty straightforward to learn, use and implement. SQLite is a relational database management system just like Oracle, MySQL and PostgreSQL. Due to its small footprint and public domain license, it is possibly the most widely deployed database engine in the world, it can be found in all types of software ranging from embedded systems, browsers, and operating systems.

Android has a built in SQLite implementation, and application specific database files are stored in a private disk space that’s inaccessible to other applications. This way, no application can access another application’s data.
Preparation
The sample application for this tutorial shows how to create a database, named “SQLiteExample.db”, that contains a single table named “person”. This table stores Person data, including his name, gender and age.
There are two Activities, MainActivity, which shows a list of stored Person names, and CreateOrEditActivity, which allows adding and editing Person details.

The most important class, in our sample, is the ExampleDBHelper class, which extendsSQLiteOpenHelper. SQLiteOpenHelper is a helper class designed to manage database creation and version management. You override onCreate() and onUpgrade() methods, and whenever a new database is created or upgraded, the appropriate method gets invoked.
ExampleDBHelper is where all the SQLite operations are carried out, and both MainActivity and CreateOrEditActivity call methods from this class to view, create, update or delete data from the database.

Extending “SQLiteOpenHelper”
We create a class, called ExampleDBHelper, that extends SQLiteOpenHelper. We begin by defining the database, tables and columns as constants. This is always a good idea. If any of these names get changed, rather than hunting through the source for all occurrences, we simply change it once. Take special notice of the column called “_id” (PERSON_COLUMN_ID). This column has special significance which will be discussed below.
In the constructor, we call SQLiteOpenHelper’s constructor, passing it the application context, the database name, an SQLiteDatabase.CursorFactory (we actually pass a null object here), and the database version. This constructor handles the creation or upgrade of the database. The database version should begin from 1, and increase linearly, whenever you modify the database schema.

The onCreate() is called whenever a new database is created. Here, you specify each table schema. In our example app, we have only one table.
The overridden onUpgrade() method is called whenever the database needs to be upgraded (i.e. when the version has changed). Here, you should drop and/or add tables, or migrate data to new tables, or whatever else needs to be done to move from the previous database schema to the new schema. In our example, we simply drop the existing “person” table, and then call onCreate() to recreate it. I doubt you would want to do this with real user data.
For the sample application, we want the ExampleDBHelper class to handle all data insertion, deletion, updates and views (basically all queries to the database must be through ExampleDBHelper). So we define appropriate methods for each of these scenarios.
To insert a new Person, we use the creatively named insertPerson() method. We use the SQLiteOpenHelper method getWritableDatabase() to get anSQLiteDatabaseobject reference to our already created database. The Person details are stored in a ContentValues object, with the appropriate column name as key, and corresponding data as value. We then call SQLiteDatabase’s insert method with the person table name, and the ContentValues object. NOTE that we left out the PERSON_COLUMN_ID column, which was specified as a primary key. It automatically increments.
Deleting data is also pretty straightforward. SQLiteDatabase has a delete() method that takes the table name to delete from, and optional whereClause and whereArgs. NOTE: Be very careful when writing this, as passing null in the whereClause would delete all rows.
Integrating the “Helper” with the rest of the app
At this point, we have completely prepared our database for use. To integrate this with both our activities, we initialize ExampleDBHelper in each Activity onCreate() method, and then call the appropriate method, depending on what action is required.
In MainActivity, we display a list of all persons using a ListView. Clicking on the “Add New” button starts CreateOrEditActivity in create mode, allowing us to create a new person. While clicking on any list item starts CreateOrEditActivity in edit mode, allowing us delete or edit and update the person info.
To display the list of persons, we request a Cursor containing all person records. We then use a SimpleCursorAdapter instance for displaying the list items. Do you remember the special column named “_id” mentioned above? SimpleCursorAdapter requires this column, or else it would not work. To use the SimpleCursorAdapter, we define a layout view for each list item called person_info.xml.
All the magic in MainActivity happens in the onCreate() method. We get a dbHelper object, request for all person’s, and populate the ListView with the received Cursor. Finally, we implement the onItemClickListener() so that when a person is clicked, we get the person ID and parse it as an extra to CreateOrEditActivity.The complete MainActivity code follows:
CreateOrEditActivity is a little bit more involved. The Activity allows creating, editing and deleting persons. It also changes its UI based on what action is to be performed. Because of it’s length, we shall only show and discuss the parts relevant to database interaction.
In onCreate(), if we receive a personID, we call dbHelper.getPerson() with that ID, and then populate the fields with the person details:
If the delete button is clicked, we call dbHelper.delete() with the personID
Finally, we implemented a persistPerson() method that checks if we require a person creation or update.
Conclusion
Extending SQliteOpenHelper helps you abstract the database creation, updates and maintenance, so you can focus on interacting with the database. We used different methods to interact with the database, including helper methods, and plain SQL commands.
While this sample app creates a simple one-table database, SQLite is a complete relational database system, and supports more advanced features including multiple tables, foreign keys, complex queries with joins and more. Using SQLite in your android app is a fast, secure, efficient and easy way to persist user and app data.
The complete source code for this tutorial is available onGitHub.
Thank you for being part of our community. Read ourComment Policybefore posting.