Android SQLite Database tutorial | Insert,Delete,Upate,Select

 

In this Android SQLite Database tutorial we are going to create a simple app which performs various crud operations like insert, update, view, delete. We are going to use RecyclerView to list the data in the database.

I will try my best to make you understand even the basics of android development through this tutorial. In my previous tutorial i have shown you how to create android Recyclerview and load them with images from server using libraries like volley and Glide.

In chance you have not seen the post, you can visit the post by clicking this link – Android RecyclerView using Volley and Glide Android tutorial. If you like this Android SQLite Database tutorial then I’m sure that you will like the other posts too 🙂

Steps involved in this Tutorial are:

I. Creating new project.

II. Adding dependencies in the gradle file.

III. Creating java file DatabaseHelper to create and edit database.

IV. Creating classes required to implement RecyclerView.

V. Layout design(we are using simple material design nothing complicated).

VI. Implementing the recyclerview in activity loaded with contents of the database.

1.Creating the project and adding resources

1. Open Android studio click on file->New->NewProject then enter the details of the project like name, backward compatibility, and package name.

2. Create an empty activity and name it the default one which is MainActivity (I just want to make it simple) then click finish. Now you have created your project. Make sure that there is no error by tapping on the RUN button at the top.

 3. Open the app-level build.gradle add following lines of code under the dependencies tag.you can find the project under app folder

build.gradle

4. In the styles.xml file located under res->values change the line

Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light.NoActionBar to hide the default actionbar.

styles.xml

The image resource im using in this tutorial is three images for the add, delete and edit operation

You can get those images from the Android studio’s inbuilt vector creation tool. check the image below for more reference.

 

2.Creating Layout files

5. Now lets create the layout files for building the UI.we need two layout files activity_main.xml the main layout file and recycler_content.xml which we will use to design the content of recyclerview.activity_main.xml will be created by default, you can create recycler_content.xml by right-clicking on layout folder under res directory layout->new->layout resource file.

activity_main.xml

recycler_content.xml

 

3. Creating Java code

6. The java file MainActivity.java will be generated by default.we have to create following classes.you can create these classes by right-clicking yourpackagename->new->java class.

  1. DatabaseHelper(Helper class to create and manage database).

      2. Data(Model class).

      3. Adapter(Adapter used to add RecyclerVIew to the main activity).

      4. AddData(Used to add data to database).

      5. EditData(Used to edit data in database).

  1. Now lets start by adding code to the recently created DatabaseHelper class in which we perform our various database functions.

 

DatabaseHelper.java

7.First lets initialize the variables which later we will use to create database.also add the constructor for our class.

Add this two method to create database and delete database if already exists.

8. Now we are going to add below methods in DatabaseHelper.java to perform crud operations in our android database.

This is how our final DatabaseHelper.java  looks like.

9. Now we have edited the Helper class lets move to the Recycler part. I’m not going to explain each every steps in this because its way out of scope of this tutorial. lets edit our Adapter class and Data class.Add the following code to the model class Data.java.

Data.java

Adapter.java

10. Create two classes AddData and EditData and add following code.

AddData.java

EditData.java

11. And finally add code to our MainActivity.java.

MainActivity.java

final image should look something similar to this.

Congratulations!! You have created your first SQLite android application.You like my content, and want more? please support me back by sharing this content to your friends.

If you ran into any error don’t worry just put the screenshot below at the comment box is always available to help. If you have not seen my previous posts please do check it out. I believe this content was useful to you if so I’m sure that those post will be helpful to you as well.