Javatpoint Logo
Javatpoint Logo

Kotlin Android SQLite Tutorial

SQLite is an open-source relational database that is used to perform database operations on Android devices such as storing, manipulating or retrieving persistent data from the database.

By default SQLite database is embedded in android. So, there is no need to perform any database setup or administration task.

The SQLiteOpenHelper class provides the functionality to use the SQLite database.

SQLiteOpenHelper class

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.

Constructor Description
SQLiteOpenHelper(context: Context, name: String, factory: SQLiteDatabase.CursorFactory, version: Int) Creates an object of SQLiteOpenHelper for creating, opening and managing the database.
SQLiteOpenHelper(context: Context, name: String, factory: SQLiteDatabase.CursorFactory, version: Int, errorHandler: DatabaseErrorHandler) Creates an object of SQLiteOpenHelper for creating, opening and managing the database. It specifies the error handler.

Methods of SQLiteOpenHelper class

There are several methods available in the SQLiteOpenHelper class. Some of them are mentioned below:

Method Description
public abstract void onCreate(SQLiteDatabase db) Called only once when the database is created for the first time.
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) Called when the database needs to upgrade.
public synchronized void close () Closes the database object.
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) called when the database needs to downgrade.

SQLiteDatabase class

It contains methods to be performed on the SQLite database such as create, update, delete, select etc.

Methods of SQLiteDatabase class

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

Method Description
execSQL(String sql): Unit Executes the SQL query, not a select query.
insert(String table, String nullColumnHack, ContentValues values): Long Inserts a record on the database. The table specifies the table name, nullColumnHack doesn't allow completely null values. If the second argument is null, android will store null values if values are empty. The third argument specifies the values to be stored.
update(String table, ContentValues values, String whereClause, String[] whereArgs): Int Updates a row.
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy): Cursor Returns a cursor over the resultset.

Kotlin Android SQLite Database CRUD Example

In this example, we will perform create, read, update and delete operation on Android SQLite database.

activity_main.xml

In the activity_main.xml file add the following code. In this file, we added three EditText, one ListView, four Button for saving, view, update and delete operation.

MainActivity.kt

Add the following code in the MainActivity.kt class. In this class, the saveRecord() function saves the records. The viewRecord() function reads the record and displays them into ListView, the updateRecord() function updates the record on the basis on id, and deleteRecord() function deletes the record. The val databaseHandler: DatabaseHandler= DatabaseHandler(this) creates the instance of DatabaseHandler class calls the SQLite database logic.

EmpModelClass.kt

Creating a data model class named as EmpModelClass.kt

custom_list.xml

Create a custom row layout for displaying the list items in the ListView.

MyListAdapter.kt

Now, create a custom adapter class named as MyListAdapter.kt and extends ArrayAdapter class which populates the data model into the ListView.

update_dialog.xml

Create a layout for display AlertDialog for update record.

delete_dialog.xml

Create a layout for display AlertDialog for delete record.

DatabaseHandler.kt

Create the DatabaseHandler.kt class that extends SQLiteOpenHelper class and override its onCreate(), onUpgrage() functions. Insert data into the database by passing a ContentValues object to the insert() method.

Output:

Kotlin Android SQLite Tutorial Kotlin Android SQLite Tutorial
Kotlin Android SQLite Tutorial Kotlin Android SQLite Tutorial
Kotlin Android SQLite Tutorial Kotlin Android SQLite Tutorial
Kotlin Android SQLite Tutorial Kotlin Android SQLite Tutorial
Kotlin Android SQLite Tutorial
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA