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.

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:

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.