This is the first part of the SQLite for Android series that we are going to explain how to create simple data bases, add records, update records and delete data from it. Along the path of experiencing the data base handling in Android we will explain every detail regarding data handling in you Android App.

This first part of SQLite for Android, is just getting to know how to create a simple SQLite for Android database in your app.

ButterKnife library

First we add our favorite ButterKnife library in our build.gradle app file and sync the project:

DataBaseHelper class

In order to use SQLite database, first we will need to create it. To create database, we will need to create our own database Helper Class. In our example it is called DataBaseHelper and will extend SQLiteOpenHelper:

Now we need to implement methods from SQLiteOpenHelper and implement default constructor:

Database meta-data

We need a database name, name of table, columns and version. This fields are required so we can make a functional database. Every one are static final so they will be immutable:

Create the Database

For simplicity, we will modify our constructor so it will take only Context as parameter and give database name and version, Factory can be null. Also we will initialize the database here just for this example. Later it will be removed:

Creating table in database

In onCreate(SQLiteDataBase sqLiteDataBase) method we create the table of our previously created database. Inside onCreate there is argument of sqLiteDataBase database that we can use:

execSQL execute any query for the database, and in this we have CREATE for creating the table with name STUDENT_NAME, and append column 1 which is ID and is auto incremented, column 2 is NAME, column 3 is LAST_NAME, column 4 is DATE_OF_BIRTH and column 5 is GENDER. The ID is INTEGER and all others are TEXT fields. This is standard SQL syntax for creating table.

Upgrade database

If we need to upgrade the table we call onUpgrade() method. First we must check if our table exists, and if it does we are DROP -ping it (remove) and create new table calling onCreate. Again we are using execSQL method. In onUpdate, as a argument, we have our previously created database – sqLiteDatabase, that we have created in onCreate:

Initialize DataBaseHelper

In our MainActivity we declare DataBaseHelper with name dataBase. Initialization of this reference is in onCreate in MainActivity and we pass the context of the activity when we create the new DataBaseHelper object. DataBase is declared globally in MainActivity for future usage in the next parts of this tutorial:

Now, when we start our app, in MainActivity in onCreate method, we create new dataBase file. To see that we actually did create a .db file we need to find it on our phone.

To do this, go to Android Studio –> View -> Tool Windows -> Device File Explorer OR in your bottom right corner of the Android Studio in vertical format. In here youll see your phones File Explorer. Select folder data –> data –> <yourapp>(example for this app is com.denofdevelopers.blogdbmanager_partone) –> databases –> Person.db. If there is this .db file, you have successfully created a database.

Continue to Part 2 – Inserting values in to database

You can get the whole project on GitHub BuktopMKD/SQLiteDatabaseAndroid

Spread the love

1 Comment

  1. I’m really enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Excellent work!|

Leave a Reply

Your email address will not be published.