Android SqLite Database Sample

buraknalpara.com

In this post, we are going to see how to use SqLite database in our android applications. I will create an android SqLite android Database Sample application with Create database, Add, Update, Delete and Get operations. After reading this post you will be able to implement CRUD operations to your own android applications easily.

How to create SqLite Android Database ?

Ok let’s start with opening a new empty project on android studio or whatever you are using for android development. I have created a database called “Students” which will contain student name and age inside it. you can prefer to use either “Students” or give another name to your SqLite database.

Write below code first to create database under OnCreate method of your application.

SQLiteDatabase dbStudent = this.openOrCreateDatabase("Students", MODE_PRIVATE, null);

This code piece will either open or create database. If it is already exists before it will not create another database with same name.

Now, we should create our table, I will name it student again to make it clear. I have created a function for each functionality.

Here is the code for creating table for students.

public void CreateTable(SQLiteDatabase db){
       db.execSQL("CREATE TABLE IF NOT EXISTS students(name VARCHAR, age INT(2))");
       Log.i("SqLite", "--------------------");
       Log.i("SqLite", "students table Created");
       Log.i("SqLite", "--------------------");
   }

I have added logs to see them in the logcat after running the application.

After creating table it is time to add some data into our students table, I have added 3 data for now if you want you can add more data to your database. Here is the code for adding data to the table.

public void AddData(SQLiteDatabase db){
    db.execSQL("INSERT INTO students (name,age) VALUES ('Burak', 25)");
    db.execSQL("INSERT INTO students (name,age) VALUES ('Buraknalpara', 35)");
    db.execSQL("INSERT INTO students (name,age) VALUES ('Nalpara', 45)");
    Log.i("SqLite", "--------------------");
    Log.i("SqLite", "students added");
    Log.i("SqLite", "--------------------");
}

Then we will update one of our data. I have updated the age data of Burak. It was 25 before and after update process it should be 30. Below you can see the code for update function.

public void UpdateData(SQLiteDatabase db){
     db.execSQL("Update students Set age = 30 where name  = 'Nalpara'");
     Log.i("SqLite", "--------------------");
     Log.i("SqLite", "student data updated");
     Log.i("SqLite", "--------------------");
 }

Now let’s look at our delete function. I have deleted the data whose name started with “Bur”. Please see beow code for delete function.

public void DeleteData(SQLiteDatabase db){
    db.execSQL("DELETE from students where name like 'Bur%'");
    Log.i("SqLite", "--------------------");
    Log.i("SqLite", "Student Deleted");
    Log.i("SqLite", "--------------------");
}

Last but not least we also have Get function to read data. I have called this function after each operation so we can see the effect of our functions on logcat. Here is the code for getting datas.

public void GetData(SQLiteDatabase db){
      Cursor cursor = db.rawQuery("Select * from students",null);

      int nameIx = cursor.getColumnIndex("name");
      int ageIx = cursor.getColumnIndex("age");

      cursor.moveToFirst();

      while (cursor != null){
          Log.i("SqLite", "--------------------");
          Log.i("SqLite", "Get students called");
          Log.i("SqLite", "--------------------");
          Log.i("SqLite", cursor.getString(nameIx));
          Log.i("SqLite", Integer.toString(cursor.getInt(ageIx)));
          cursor.moveToNext();
      }

  }

We have finished our SqLite Android sample application. You will see results of functions on the logcat as shown below.

SqLite sample android

Please seeĀ https://github.com/brknlpr/androidSqLiteSample for full code of this application. If you want check Alert Dialog in Android please click the link also.

HBN

 

 

5 (100%) 8 votes

You May Also Like

About the Author: HBN

--Senior QA Engineer-- --Intermediate Web Developer-- --Junior Android Developer--

Leave a Reply

Your email address will not be published. Required fields are marked *