Start a Project

Using SQLite Database in android for local storage

Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS.
SQLite is an open-source relational database i.e. used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database.
It is embedded in android bydefault. So, there is no need to perform any database setup or administration task.
 
SQLiteOpenHelper class
SQLiteOpenHelper class provides the functionality to use the SQLite database.
 
The android.database.sqlite.SQLiteOpenHelper class is used for database creation and version management. For performing any database operation, you have to provide the implementation of onCreate() and onUpgrade() methods of SQLiteOpenHelper class.
 
Constructors of SQLiteOpenHelper class
 
There are two constructors of SQLiteOpenHelper class:

i. SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

creates an object for creating, opening and managing the database.

ii. SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)

creates an object for creating, opening and managing the database. It specifies the error handler.
 
Methods of SQLiteOpenHelper class:

There are many methods in SQLiteOpenHelper class. Some of them are as follows:
i. public abstract void onCreate(SQLiteDatabase db)
-called only once when a database is created for the first time.

ii. public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
-called when the database needs to be upgraded.

iii. public synchronized void close ()
-closes the database object.

iv. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
-called when the database needs to be downgraded.
 
SQLiteDatabase class

It contains methods to be performed on sqlite database such as create, update, delete, select etc.
 
Methods of SQLiteDatabase class

There are many methods in SQLiteDatabase class. Some of them are as follows:

Method Description

i. void execSQL(String sql)
-executes the sql query not select query.

ii. long insert(String table, String nullColumnHack, ContentValues values)
-inserts a record on the database. The table specifies the table name, nullColumnHack doesn’t allow completely null values. If second argument is null, android will store null values if values are empty. The third argument specifies the values to be stored.

iii. int update(String table, ContentValues values, String whereClause, String[] whereArgs)        -updates a row.
iv. Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
-returns a cursor over the resultset.
 
Example of SQLite Database with few simple steps:
 
1. Create an Activity:

 
2. Create a class and extend SQLiteOpenHelper class in it:

 

Exit mobile version