Android SQLite Database Complete Guide

Let’s discuss about what is sqlite database in android. SQLite is an open source database. You can use it free. It supports all the standard features that a relational database has. So, you can say it a relational database. The most important feature of SQLite databse is its a local database. It doesn’t reqire any server, any ODBC or JDBC query or connection.

SQLite also supports data types like TEXT, REAL, INTEGER same as STRING, DOUBLE and LONG in C++ respectively. Yeah! you have to specify data type (mentioned above) before saving data into database.

It uses text file to save your data and the text file locally store in your Android device.

SQLite is embedded into every device running Android OS. Android devices come up with built-in SQLite database implementaion. So, you don’t need to install any extra libraries to use SQLite database with Android.

Creating SQLite Android Database

To create database in your Android application you just need to create a child calss of the SQLiteOpenHelper class.And you have to call the super() method of the parent class SQLiteOpenHelper by specifying your database name and current version in the constructor of your child calss.

Here you need to override the following methods to create and modify database.

1-onCreate(SQLiteDatabase db) – This method is used to create a database from scratch and to open created database.

 

2-onUpgrade() – use to modify existing database or to drop the existing database.

 


Both methods recieve an object as parameter of SQLiteDatabase class.

There are also two methods   getReadableDatabase() and  getWriteableDatabase() of SQLiteOpenHelper class use while read or write data.

Insert value in Android SQLite Database

To insert values in database we use insert() method/function of SQLIDatabase class.Insert()

method returns two values, if your statement successfull it will return you “row inserted” and if your statement goes wrong it will return -1.You can either show the same value as method retunrs to the user or you can also show a different message to user.


SQLiteDatabase class provide  execSQL() uses to execute any SQL statement directly.

Select Data from SQLite Database

To select/retrieve data from database we use Cursor class object.We call a method rawQuery from Cursor class and it returns what we select from table in the form of query.

There are other functions of Cursor class to get data about the table.Some funtions are given below…

getColumnsCount() – count the total number of columns in the table

getCount() – returns the total number of rows in the table

getColumnIndex(String columnName) – returns the index number of given column name

getColumnNames() – returns the array of all column names in the table

isClose() – returns true if the cursor is closed otherwise false

getPosition() – returns the current position of the cursor in the table

Modify values in SQLiteDatabase

To update/modify values in SQLite database we will use the update()  method of  SQLiteDatabase class.In the parameter body of the update() method we will pass four parameters TableName, ContentValues, whereCaluse, and String[] whereArgs.

Delete values from Database

To delete values from our database here also we use the delete() method of SQLiteDatabase class.In the update() method we pass four parameters but in delete metho we pass three parameters.We will not pass Contentvalues as paramenter.

Leave a Reply