Learn to save data with SQLite on Android

On Android, there are several solutions to persist data between users’ sessions. One solution is to use a relational database to persist data and then to be able to query easily these data. Note that this tutorial is also available in video on Youtube :

In standard, Android SDK comes with a SQLite implementation. Biggest advantage of SQLite integration to Android OS is the fact that there is no need to to setup the database. So, no administration of this database. SQLite is embedded in standard and each application can have its SQLite database.

The only job that developers must make is to define SQL tables and statements for creating and updating data. Access to an SQLite database involves accessing the file system. So, it can be a slow operation. To avoid ANR (Application Not Responding) errors, it’s recommended to perform database operations asynchronously.

When an application creates and uses a SQLite database, it will be saved by default in the directory : DATA/data/APP_PACKAGE/databases/FILENAME .

1. Architecture

All classes needed to manage databases in Android SDK are contained in the package android.database . The package android.database.sqlite contains the SQLite specific classes.

SQLite API is centered around 2 main classes :
- SQLiteOpenHelper that is an helper class to extend to manage database operations.
- SQLiteDatabase that is the base class for working with a SQLite database in Android.

2. SQLiteOpenHelper

When you want to work with a SQLite database in Android, you must extend SQLiteOpenHelper class. In the constructor of your subclass you call the super() method of SQLiteOpenHelper, specifying the database name and the current database version.

You need also to override the following methods :
- onCreate() that is called when database is accessed but not yet created.
- onUpgrade() called when you choose to increment the version number of the database. In this method you can manage the migration process between two databases versions.

Both methods get and SQLiteDatabase instance in parameter which is the way to communicate with the database.

Furthermore, SQLiteOpenHelper provides 2 methods to get access to an SQLiteDatabase instance object respectively in read and in write modes :
- getReadableDatabase() for read mode.
- getWriteableDatabase() for write mode.

3. SQLiteDatabase

SQLiteDatabase is the class used to communicate with a SQLite database. It exposes several methods to interact with database like insert(), update() or delete().

In addition, it lets you to make queries via rawQuery() to queries made directly in SQL or via query() method. This last method provides a structured interface for specifying a SQL query.

4. Practice

Now, you know theory about SQLite in Android context. We can put in practice all the concepts. To achieve that, we’re going to make a database with a players table letting us to store NBA players.

To start, we create a simple Player Java POJO :

Then, we must create the SQLiteOpenHelper extended class to manage our application database. Code is here :

Database is created in the constructor of the extended class. Players table is created in the onCreate() method thanks to a SQL statement.

In our class, we add methods to add a new player, to delete an existing one, to update and then a method to get all the players in the table. In this last method, we use a Cursor object to iterate on rows and then build equivalent Player instances.

To use our class to create some players then display on a simple ListView, we can use the following code :

Execution result can be seen here :

Image for post
Image for post

SQLite implementation in Android is simple and really powerful. You can now use it in your Android application to persist data.

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store