SQLite Database with ContentProvider in Android

ContentProvider is one of the main component of Android Application. Content Providers are the wrappers to expose data stored locally or remotely. ContentProviders are written like REST like URIs for example to retrieve n image from the Gallery, the URI to create might be like this:

content://media/external/images/media/123

Android comes with built in Providers for storage, media, contacts. Let’s see in this post how to create ContentProvider for SQLite backed storage. In order to create ContentProvider you need to extend ContentProvider and implement methods like insert, query, delete, update etc. We will also extend SQLiteOpenHelper that will implement helper functions to create SQLite database.

We will be creating a basic Notes app with functionalities to create, delete and update notes.

Init ContentProvider

AndroidStudio provides a direct menu option to create bare bone ContentProvider class. Create an empty Android project and open FIle -> New -> Other and select Content Provider

Replace URI Authorities with your unique id. The common approach is to replace package id with your project package id. After finishing the configuration AndroidStudio will create ContentProvider class with details passed in the previous step.

Create Database

Next step is to initialize and create SQLite database. Create a new class and extend it from SQLiteOpenHelper

Now we have our class ready to create database for notes. The next step is to design the database and define table to store note object.

Open DatabaseHelper class and create Notes database table.

DatabaseHelper constructor create database for us and implement onCreate function to create tables. Once DatabaseHelper is ready we can override onCreate function of ContentProvider to initialize our DatabaseHelper class.

Define our AUTHORITY and CONTENT TYPES in ContentProvider.

Create URI Matcher that is used to distinguish one URI from other. We will also define projection map which is similar to SQL “as” command. ProjectionMaps are used to rename the columns in the query results.

Override getType function to handle requests for the MIME type of the data.

Override onCreate to initialize database. This will create database with version and tables when the app is run for the first time.

Update insert function  which takes ContentValues as the parameter.  We take the reference of database and executes insert query to add a new row in the database. If new row is created then we notify all the observers.

Now we can jump to query and see how to query all the notes or get details of a particular note.

Now our content provider and database is ready for actual query operations. Lets see the code to query and insert notes from activity.

Create Notes object.

I am providing code snippets to add and list notes from the database.

Pre-Populated SQLite Database

So far we have seen how to create our database in Android Application. One may ask, what if I need to use a pre populated database in the app?

We can also ship our app with a pre filled database. In such scenario we need to keep the pre filled database inside assets folder and copy your database from local assets folder to apps newly created empty database in the system’s folder.

Let’s say you have saved your local database inside assets folder as notes.db

Update DatabaseHelper class to support copying local database to system database.

Update ContentProvider to initialize database helper.

 

 

Leave a Reply

%d bloggers like this: