Android SQLite Database Crud(Insert,Delete,Update,View,Search) Tutorial with Example

By | July 10, 2019
Sqlite Database in Android Tutorial Example

What is Sqlite Database in Android ?

Sqlite Database is an open source relational Database.It is used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database.

It is embedded in android by default.  No need setup any database.

SQLite supports all the relational database features. Android is Built in Database Implementation.

No need to any connections of like JDBC,ODBC e.t.c.

SqliteOpenHelper class provides the SQLite database functionality.

SqliteOpenHelper Constructor

There are two type of Constructor.

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

In this constructor used to creates an object for creating, opening and managing the database.

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

In this Constructor used to creates an object for creating, opening and managing the database. It specifies the error handler.

Methods of SqliteOpenHelper Class

public abstract void onCreate(SQLiteDatabase db) : In this Method used to database is created for the first time.

public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) : In this Method is used to database needs to be upgraded.

public synchronized void close () : In this Method is used to closes the database object.

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) : In this Method is used to database needs to be downgraded.

Methods of SQLiteDatabase

void execSQL(String sql)

In this method used to executes the sql query it is not select query.

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

In this Method used to Inserts record on the database And  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.

int update(String table, ContentValues values, String whereClause, String[] whereArgs) : In this Method used to Update row in the Database table.

Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) : In this Method used to Retrieve Data from Database and it can return cursor. retrieve anything from database using an object of the Cursor class and it will return a resultset with the cursor pointing to the table.

Example Of SQLite Database In Android

Create activity_main.xml File Follow this Code.

Create MainActivity.java File Follow this Code.

Create activity_add_contact.xml File Follow this Code.

Create AddContactActivity.java File Follow this Code.

Create activity_update_contact.xml File Follow this Code.

Create UpdateContactActivity.java File Follow this Code.

Create activity_delete_contact.xml File Follow this Code.

Create DeleteContactActivity.java File Follow this Code.

Create activity_search_contact.xml File Follow this Code.

Create SearchContactActivity.java File Follow this Code.

In this Example of I have Create Insert,Delete,Update and Search Contact from SQLite Database Stored. it can different different activity to perform database related operation. first of all create Database Helper File and then implements SQLite Database and then override methods implement and i have create database name,database version.

In this Database file to create some methods to insert data delete data search data from database. and then called this method to different different activity and then perform activity task.

It is Simple Example of SQLite Database to stored contact form detail on the database table.and then delete search and update this information to access on database.