Android SQLite Database Tutorial

This article is about android sqlite database tutorial.

There are several storage options available in android like shared preferences, internal and external storage, sqlite, etc. Here we will see how to use sqlite database as a storage system in android to perform CRUD operations.

SQLite is light weight open source database that stores data in text files. Android already comes with built in sqlite database.

Also Read: Java SQLite Tutorial

Android SQLite Database Tutorial

 

Android SQLite Database Tutorial

The database table that I will use in this tutorial has following structure.

Table Name: record

Field Type
id integer, primary key, autoincrement
name text

 

  • The android.database.sqlite package contains sqlite specific classes.
  • The SQLiteOpenHelper class provides all the functionality for sqlite database.
  • The SQLiteDatabase class provides various methods to perform create, read, update and delete operations.

 

Create Database

Before working with SQLite database we have to first extend SQLiteOpenHelper class. The example that I have used here contains DBHelper class that extends SQLiteOpenHelper class and perform all database related operations.

For creating the database we will call constructor of SQLiteOpenHelper class using super().

 

onCreate() and onUpgrade()

We have to override two methods onCreate() and onUpgrade().

The code required to create table will be written inside onCreate() method.

 

onUpgrade() method contains the code required to update the database.

 

Insert

In this example the insert operation is handled by insertRecord() method. It takes name as a string argument and insert it into table. We have to first add all the values in ContentValues object and then finally insert into table using insert() method of SQLiteDatabase class.

 

Read

For reading from table we just execute our select query using rawQuery() method of SQLiteDatabase class. This method returns Cursor object that will be used to fetch the records one by one.

 

Update

The update() method of SQLiteDatabase class is used to perform update operation according to a primary key. In this example id column is the primary key.

 

Delete

The delete operation is performed by delete() method of SQLiteDatabase class according to primary key.

 

Android SQLite Database Example

First create a new project with name AndroidSQLite and package name com.androidsqlite.

Add following code in respective files and run the project.

Android SQLite Database Tutorial

MainActivity.java

 

DBHandler.java

 

activity_main.xml

 

Output

Android SQLite Database Tutorial

 

If you are facing any problem related to above android sqlite database tutorial then feel free to comment below. I will try my best to solve your problem.

3 thoughts on “Android SQLite Database Tutorial”

  1. Dear Neeraj,

    Firstly, I want to thank you for your tutorial. It is quite clear for beginners like me. I would you like to ask a few questions;

    – I created a dictionary database (_id, word, wordtype, definition). How could I use it in your example with searchable listview?
    – I would like to use CRUD functions in listview dictionary because I want to improve my database in daily using.(insert new words, update words, etc.)

    Please help me, also you can contact me with my email.

    Best regards.

Leave a Comment

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