Loading, please wait...

A to Z Full Forms and Acronyms

Explain SQLite in Android | Android Tutorial

Jan 21, 2022 #AndroidLanguage #Programming, 2867 Views
In this article, we will discuss the following pointers:What is SQLite?Explain in detail about SQLiteOpenHelper along with the functions. Explain in detail SQLiteDatabase class along with the functions.

Explain SQLite in Android | Android Tutorial

In this article, we will discuss the following pointers:

  • What is SQLite?
  • Explain in detail about SQLiteOpenHelper along with the functions. 
  • Explain in detail SQLiteDatabase class along with the functions. 

What is SQLite?

It is an open-source relational SQL database. It stores the data to the text file in an android device. SQLite is used to perform the various database operations in android including storing, manipulating, and retrieving data. Android has an in-built SQLite database. So, there is no need to manage or implement an external database setup.

SQLiteOpenHelper along with the functions

It is a helper class in the SQLite database. It kind of manages all the operations related to the database. It manages the automatic creation and updation of databases

For database creation and updation, we are using the android.database.sqlite.SQLiteOpenHelper. class.

We have to provide the implementation of onCreate() and onUpgrade() methods of SQLiteOpenHelper class to perform any database operations. 

The two constructors of SQLiteOpenHelper class. The definition of constructors and other methods are as follows:

Constructor and Methods

Definition

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

It creates a database object for creating, managing, and opening the database. 

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

It is used to create a database object for creating, managing, and opening the database. The main purpose of this constructor is to handle errors.

public abstract void onCreate(SQLiteDatabase db)

When the database is created, it calls this first only once.

public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

When the database needs updating, it calls this method.

public synchronized void close ()

It is used to close the database object.

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)

When the database needs to be downgraded, it calls this function. 

 

SQLiteDatabase class along with the functions

It accommodates all the methods to be performed on SQLite databases such as create, select, delete, update, etc.

Methods

Definition

long insert(String table, String nullColumnHack, ContentValues values)

It is used to insert the row in the database. If the second argument is left empty, android will store a null value. The third argument is used to specify the values to be stored.

int update(String table, ContentValues values, String whereClause, String[] whereArgs)

This query is used to update the row.

void execSQL(String sql)

This query is used to execute the SQL query instead of the select query.

Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

This query returns the cursor over the resultset.

 

A to Z Full Forms and Acronyms