SQLite Database used in Android Application Development

SQLite is a relational database management system (RDBMS). SQLite database used in Android Application Development.  It is well regarded. It is Open source, Lightweight. It uses Single-tier database architecture. SQLite is an Open Source Database which is embedded into Android. SQLite even supports standard relational database features like SQL syntax, transactions and prepared statements.
SQLite database supports three type of data type as Text, Integer, Real. You might be familiar with text and integer data type as it is basic data type, almost uses in every language. Real is one of the important datatype supported by SQLite database used in Android Application Development.

If you want to develop your own Android App, get our Full Android Application Development Tutorial FREE.

All other types must be converted into one of these fields before saving them in the database. SQLite is available on every Android device. Using an SQLite database in Android does not require any database setup or administration. You only have to define the SQL statements for creating and updating the database. Afterwards the database is automatically managed for you by the Android platform.

Packages for SQLite Database:

The package android. SQLite database contains all general classes for working with databases. Package “android.database.sqlite” contains the SQLite specific classes.

  • SQLiteOpenHelper

To create and upgrade a database in your Android application you usually create subclass of SQLiteOpenHelper. In this class, you need to override the onCreate() and onUpgrade() methods.

onCreate() is called by the framework, if the database does not exist.
onUpgrade() is called, if the database version is increased in your application code. This method allows you to update the database schema.

These two receive an SQLiteDatabase object as an it’s argument. SQLiteOpenHelper uses the methods getReadableDatabase() and getWriteableDatabase() to get access to an SQLiteDatabase object. either in read or write mode.
It is good to create a separate class per table. This class defines static onCreate() and onUpdate() methods.

  • SQLiteDatabase

SQLiteDatabase is the other base class used for working with the SQLite database in Android and provides methods to open database, query means to selecy , update and close the database. In addition, it provides the execSQL() method, which allows to execute an SQL statement directly. Queries can be created via the rawQuery() and query() methods or via the SQLiteQueryBuilder class.

query() provides a structured interface for specifying the SQL query.

query() Example

The following gives an example of a query() call.

return database.query(TABLE Name,
null, null, null, null, null);

The method query() has the following parameters.
Table 1. Parameters of the query() method

Parameter Comment
String dbName The table name to compile the query against.
int[] columnNames A list of which table columns to return. Passing “null” will return all columns.
String whereClause Where-clause, i.e. filter for the selection of data, null will select all data.
String[] selectionArgs You may include ?s in the “whereClause””. These placeholders will get replaced by the values from the selectionArgs array.
String[] groupBy A filter declaring how to group rows, null will cause the rows to not be grouped.
String[] having Filter for the groups, null means no filter.
String[] orderBy Table columns which will be used to order the data, null means no ordering.

If a condition is not required you can pass null, e.g. for the group by clause.
The “whereClause” is specified without the word “where”, for example a “where” statement might look like: “id=19 and summary=Title”.

  • Cursor

A query returns a Cursor object.Cursor is similar to ResulSet obeject in java and DataReader object in c#.net or VB.net.A Cursor represents the result of a query and basically points to first row of the result returnd by query. To get the number of elements of the resulting query use the getCount() method. To move between individual data rows, you can use the moveToFirst() and moveToNext() methods.
Cursor provides typed getDatatype() methods, e.g. getInteger(columnIndex), getString(columnIndex) to access the column data for the current position of the result. The “columnIndex” is the number of the column you are accessing.
A Cursor needs to be closed with the close() method call.
For Example:

package com.org.net;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;</p>
<p align="justify">public class contact extends SQLiteOpenHelper {</p>
<p align="justify">            public contact(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
<p align="justify">@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
String cr="Create table contact(id integer primary key,name text,number text)";
<p align="justify">            @Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
String cr="DROP TABLE IF EXISTS contact";
public long insert(ContentValues val)
long rowid=0;
SQLiteDatabase db=getWritableDatabase();

return rowid;

public  Cursor getall()
Cursor cursor=null;
SQLiteDatabase db=getReadableDatabase();


return cursor;
<p align="justify">}</p>
<p align="justify"><strong>Activity File</strong></p>
<pre class="lang:default decode:true " >package com.org.net;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;&lt;/p&gt;
&lt;p align="justify"&gt;public class MydatabaseActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
contact con=new contact(this, "ContactDB",null,1);&lt;/p&gt;
ContentValues content=new ContentValues();
content.put("name", "Gaurao");
long rowid=con.insert(content);

content.put("name", "Rupesh");

Cursor cr=con.getall();
int i=0;


Full android web application Tutorial:-

Introduction How to develope Android application

What are the Fundamental Units of Android Application

Step by Step Installation guid for android application developent

How to Build and Run the Android Application

Views in Android Application

Linear Layout in Android Application Development

Table Layout in Android Application

Relative Layout in Android Application

SQLite Database used in Android Application Development

Music Player Code for Android Application Development

How to make Phone call for Android Application with full code

Status Bar in Android Application Development with Full Code

1 Comment
  1. Amay dey says

    yeah i really liked reading this article.

Leave A Reply

Your email address will not be published.