Sqlite database example in android

In this tutorial we going to implement the sqlite database example in android let’s start with following steps.

  • Create new Project
  • Go To File >New > New Project
  • Give name of your application and select blank activity after that click finish
  • First we create the Layout for the project

activity_main.xml

Create 4 edittext views for getting data from user. than create 4 buttons and named insert,delete,update and view record.

SQLite Database Demo

sqllite delete

 

MainActivity.java

First declare the objects for edittext and buttons than we created SQLiteDatabase instance named db. for adding functionality to buttons, implement  OnClickListeners to this class. create method named init() to create database using openOrCreateDatabase method of SQLiteDatabase class than to initialize all edittext and buttons inside it and called it from  onCreate() method. after that to implementing interface need to override  onclick method.

 

string.xml

Set string resources for the used in application

 

DatabaseHelper.java

Extend SQLiteOpenHelper class to this class for handling createing and handling database methods. first declare string objects that’s used further in this class. for creating data table override onCreate() method and write code for creating table.

 

Declare variables and after that create methods to get values of variable and sets value to variables.

Student.java

Create activity from file > new >Activity >Empty Activity and give name to this. add the listview for show the student list.

activity_student_list.xml

Create activity from file > new >Activity >Empty Activity and give name to this. add the listview for show the student list.

StudentList.java

Initialize the views and create object of MyBaseAdapter.java class to set adapter to listview.

MyBaseAdapter.java

Extends BaseAdapter class to this class and than implements it’s methods.

activity_update.xml

Create activity from file > new >Activity >Empty Activity and give name to this. add the edittexts and button

 

sqllite update

 

Update.java

Class for updating students data. first initialize all views and set onClickListners for buttons.

 

 

activity_view_records.xml

Create activity from file > new >Activity >Empty Activity and give name to this. add the listview for show the student records.

 

sqllite view

 

ViewRecords.java

 Class for showing all records of students that saved into sqlite database. create object of DatabaseHelper class and than fetch all records of students in arraylist using fetchallrecord() method of DatabaseHelper class. and than create object of MyBaseAdapter class  and set arraylist to this. after that add adapter to listview.

  • navneet dudhagara

    Really helpfull, thanks for sharing