SQLiteOpenHelper Tutorial – Performing CRUD in SQLite

Hey guys, In the last post we learned about performing the basic CRUD operation in SQLite Database. But the method we used in the previous tutorial, is not recommended. Instead, we should use SQLiteOpenHelper class. And that is why we have this SQLiteOpenHelper Tutorial. We will do the same thing that we did in the last post, but with SQLiteOpenHelper class.

If you directly came to this post, then it is highly recommended that you should go to the previous post first, as here I am going to work on the last project, so before you start, you need to get the last project. You can visit the previous post from the link given below.

Android SQLite Database Example – CRUD Operation in SQLite

What is SQLiteOpenHelper?

It is a class found inside android.database.sqlite package. It is a helper class that helps in creating the database, handling the operations and also the version management.

To use sqliteopenhelper, we will create a class, and then we will extend SQLiteOpenHelper inside the class that we created.

Creating and Managing Database with SQLiteOpenHelper

  • So open the project we created in the last tutorial.
  • Now create a class, you can name it anything, and I have given the name as DatabaseManager.
    • Inside this class we need to override onCreate() and onUpgrade() method. Inside onCreate() we will create the tables required and inside onUpgrade() we can upgrade the database (like we can add more tables).
    • We will also define the methods for all the operation that we want to be performed in our database.
  • Below is my DatabaseManager class having all the methods to perform CRUD. The database is same as it was in the previous tutorial.

Modifying the Last Project

  • In the last tutorial we had the following classes.
    • Employee.java -> The model class to store employee. We don’t need any changes here.
    • EmployeeActivity .java-> The activity where we are displaying all the employees.
    • EmployeeAdapter.java -> The custom list adapter for our Employees List.
    • MainActivity.java  -> Here we are inserting Employee to the database.
  • We will modify EmployeeActivity.java, EmployeeAdapter.java and MainActivity.java.

Creating the Employee

  • So let’s start with MainActivity.java. I have added the comments on the lines that is modified or added.

Reading All Employees

  • Now come to EmployeeActivity.java.

Updating and Deleting the Employees

  • Lastly come inside EmployeeAdapter.java and modify it as below.

  • That’s it now you can run your application and it will behave the same as it was in the previous tutorial.
SQLiteOpenHelper Tutorial
Android SQLite Database Example

SQLiteOpenHelper Tutorial Source Code

  • If you are having troubles creating the application then don’t worry, you can download my project from the link given below.

So that’s all for this SQLiteOpenHelper Tutorial friends. Hope you liked it if you did, then please SHARE it. Thank You 🙂

6 thoughts on “SQLiteOpenHelper Tutorial – Performing CRUD in SQLite”

  1. **Helper.kt->**


    class Helper(context: Context) : SQLiteOpenHelper(context, DBNAME, null, DBVERSION) {

    companion object {
    private val DBNAME = "myDb"
    private val DBVERSION = 1
    }

    –>this class will take context argument but in fragment it is showing error for “context”,”activity”,”this”.. i have tried all the things but not be able to resolve the error

    **HomeFragment.kt->**


    class HomeFragment : Fragment() {

    companion object {
    const val KEY = "data"
    }

    val helper = Helper(context) //getting error-> Required:Context , Found:Context?

    Reply
  2. What if i have multiple unrelated tables?
    I assume that i dont want to put all crud operations on all tables in one dbhelper instance?

    Would it make more sense to then create a second dbhelper?

    Or to make dbhelper just responsible for maintaining the databases and opening / closing them, and make seperate classes for handeling operations on each table?

    Reply
  3. Please add ‘mDatabase.close()’ every time you are done trying to access the database. Otherwise, the same database will be appended every time you re-open your app after destroying it.

    Reply
  4. Hello Belal, i wish you are fine. I can’t describe how you helped me by my project. Thank you soooooooooooooooo much and i wish you all the best.

    Reply

Leave a Comment