Kotlin SQLite Database Tutorial [Build Real App]

A step by step tutorial for implementing SQLite database in Kotlin. I will implement all the database operations (CRUD Operations). We will use SQLiteOpenHelper class to create and manage SQLite database.

I will develop a PhoneBook Application where we can read, insert, update and delete the contacts from SQLite database.

What you will learn from this tutorial

  • How to create and use SQLite database in Kotlin using SQLiteOpenHelper class.
  • How to create a ListView and populate it with data using Custom Adapter.
  • Implementing onClick listener on ListView in Kotlin
  • How to implement BaseAdapter in Koltin
  • How to insert, read, update and delete records in SQLite database using Kotlin.
  • How to pass data from an activity to another activity in Kotlin using Intent

Implementing SQLite Database in PhoneBook App using Kotlin

Download Source Code

Let’s start the project without wasting any time on the theoretical content.

Step: 1

Create a new project in Android Studio with default settings and name it KotlinDBTutorial. Don’t forget to check Include Kotlin support checkbox. I selected the Empty Activity from activity selection dialog box.

kotlin sqlite database

Step:2

Creating a Model Class

Create a new Kotlin class and name it ContactData. This is the model class for the database to maintain single Contact as an object.

ContactData.kt

Step:3

Creating Add/Update Contact Form

Create a new activity and name it ContactManager. In this activity, we will add, edit and delete contacts from the database. I have created a form to add/update a contact. This is a very simple form having TextView, EditText and couple of Buttons. Below is the code of ContactManager activity and its simple layout.

activity_contact_manager.xml

This is the output of above XML code. Have a look at the below add/update form

kotlin sqlite database tutorial

Step:4

Creating Database Using SQLiteOpenHelper class

SQLiteOpenHelper is used to create a database. It allows to manage database version, upgrade and downgrade the database.

Create a new Kotlin class and name it DatabaseHandler. We will implement all database operations such as insert, delete, update and read in this class.

Here is the code of DatabaseHandler class.

DatabaseHandler.kt

Companion Objects In Kotlin

There is no static member for a class in Kotlin, therefore, we use companion object. We have defined the database name, table name, column names, database version number, and database file name. The database is stored in a text file on the android device.

In Constructor(), we are setting the context passed by calling activity.
The database is created when writableDatabase method is called.

The onCreate method is called when database is created. We can create tables by executing the SQL queries.

The onUpgrade method will be called if we change the database version from 1 to 2. We are deleting existing table and create it again by calling onCreate method. We can also alter table.

AddContact method will add the records in the table.

FetchContacts will read all records in the table and return an arraylist. If we pass “%” it will fetch all data otherwise it will search the first name.

UpdateContact and RemoveContact will update and delete the contact from database. We are passing contact id to these method, which is the primary key.

Now, our database handling class is ready to use in an activity.

Step:5

Implementing ListView with Custom Adapter by using BaseAdapter class

This is the basic design of application having a ListView and Button. Open your main activity layout file and add the below code.
activity_main.xml

We need to create a layout of each cell of ListView. It is a simple layout having ImageView and TextView.

Now, we need to create a custom adapter to populate data in the ListView. Create a new Kotlin class and name it ContactAdapter by extending BaseAdapter class. Below is the complete code of custom adapter.

ContactAdapter.kt

Step:6

Now, We will use DatabaseHandler class to interact with database and ContactAdapter class to populate data into ListView.

We have an ArrayList that will hold ContactData objects.

In onResume method, We have created the instance of DatabaseHandler class and calling FetchContact method. It will returns an arraylist

ContactList is passed to ContactAdapter class via Constructor. It will fill the data into ListView.

Implementing onItemClick Listener on ListView

It is very simple to implement OnClick listener in Kotlin having less code as compare to java

OnClickListerner on Button in Kotlin

It is very simple and having less code to implement OnClickListerner on Button in Kotlin.
We can go to ContactManager activity on Button click. ContactManager activity has a form to add and update a contact.

Step:7

This is the second activity. Here is the code of ContactManager activity.

ContactManager.kt

In the activity, we are adding and updating contact data depend on the value of record_id. If it is 0 then we will add contact if it is not 0 then we will update contact as per given value of record_id.

We are using AddContact, UpdateContact and RemoveContact methods of DatabaseHandler class. We are reading data from EditText views and passing it in the form of ContentValue.

Now, App is ready to run. Run the application either on an emulator on any android device.

Project and Application Demo Video

I hope, you will like this tutorial and learn something out of it. If you have any questions or suggestion do let me know in comments section.

Some other tutorial in Kotlin, you may like to have a look. How to use AsyncTask in Kotlin

You can also subscribe to our newsletter to get new tutorials, articles, and other resources right in your inbox.

Email us your feedback and suggestion at admin@androidpala.com

Check Also

How To Fix Outdated Kotlin Runtime Problem

If you are developing Android app using Kotlin then you might have come across this …