Home Tutorials Hints & Tips

Getting Started in Android Development

Part 37 - Creating an SQLite Database Framework using the DbHelper

What is SQLite?

SQL or Structured Query Language is a popular relational database language. SQLite differs from SQL in that it is serverless and self-contained. Serverless means that it can act directly on the stored files and self-contained means that it does not rely on external libraries. This make SQLite and ideal choice to embed databases into applications. It is the database engine available to the developer of Android apps.

SQLite databases hold data in tables. The database and the tables are given names so we can access them with our app. Each row of the table is a record and the columns are the data associated with that record. The programmer defines what data the columns hold and the data type whether it be text, integer, Boolean etc.

This tutorial

We will look at SQLite over the next 5 tutorials. The idea is to leave you with a set of basic methods that can be applied to your apps. Therefore, we will develop methods to create, open and close a database. We will also create methods to add, modify and delete entries. In this part of the tutorial we will look at creating a framework for the SQLite database.

We will create a simple database to hold the name of a club member and their status whether it be gold, silver or bronze. We will need to create three java classes. One will be used to enter information into the database, one will be used to display the database and one will be used to hold all of the database methods.

The screenshot below shows the layout for the SqliteActivity.

 Android SQLite database screenshot

This class is used to enter, modify and delete the database entries and comprises two EditText fields for the member's name and status. The various buttons control the database actions. A third EditText field is used to identify the row in the database table that we wish to act on. The XML code for this layout is shown below:

The EditText fields and the buttons are setup in the java code and a switch and case statement has been created to hold code for the button actions. The code for the SqliteActivity at this stage of the tutorial is shown below:

The layout for the ShowDatabaseActivity simply comprise a couple of TextViews, one of which is used to display the database. The XML code for this layout is shown below:

The code for the ShowDatabaseActivity at this stage of the tutorial is shown below:

We need to turn our focus to the MembersDatabase.java class as this will hold all of the database methods. Methods in this class will be called by the other classes. We define a set of constants to represent the database name, the table name, database version and the column headings. The column headings will be the member’s name and their status. The key for the row ID id another column that will be used to identify the table row or record.

private final static String KEY_ROWID = "_id";
private final static String KEY_NAME= "Name";
private final static String KEY_STATUS= "Status";
private final static String DATABASE_NAME = "Members";
private final static String DATABASE_TABLE = "members_table";
private final static int DATABASE_VERSION = 1;

We also need to define some objects that will be used later on:

private final Context context;
private DbHelper databaseHelper;
private SQLiteDatabase memebersDatabase;

The class does not need to extend Activity simply because it is not associated with an Activity. The first task is to create a class constructor. Class constructors are not like other methods. They must always have the same name as the class itself. They are used to create an instance or object of the class when called on by other classes. The constructor will be referred to access the methods we are about the create. This may sound confusing, but should be clearer when we start to make calls to methods in this class. We can also place code in the constructor that is always executed ahead of the methods that we call. We only need to pass a context into our constructor. The code for the constructor is below:

public MembersDatabase (Context context) {
   this.context = context;

The next task is to create a database helper. The helper class manages the creation and version management of the database. The framework code for the helper class is below:

private class DbHelper extends SQLiteOpenHelper{
   public DbHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);

   public void onCreate(SQLiteDatabase db) {

   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

It essentially comprises two methods. The onCreate method is called should the database not exist. The onUpgrade method is called should the database pre-exist. This is all we need to create a basic framework ready for database creation in the next tutorial.

After this tutorial your MembersDatabase.java file should look similar to the one below:

Your MainActivity.java file should look similar to the one below:

Your Android Manifest file should look similar to the one below:

Download Download tutorial set 2

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved