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. …

sqliteopenhelper tutorial

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.

[sociallocker id=1372] SQLiteOpenHelper Tutorial Source Code [/sociallocker]

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

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x