![]() |
VOOZH | about |
Android comes with an inbuilt implementation of a database package, which is SQLite, an open-source SQL database that stores data in form of text in devices. In this article, we will look at the implementation of Android SQLite in Kotlin.
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. It is the most used database engine in the world. It is an in-process library and its code is publicly available. It is free for use for any purpose, commercial or private. It is basically an embedded SQL database engine. Ordinary disk files can be easily read and write by SQLite because it does not have any separate server like SQL. The SQLite database file format is cross-platform so that anyone can easily copy a database between 32-bit and 64-bit systems. Due to all these features, it is a popular choice as an Application File Format.
We will be building a simple application that will be storing data using SQLite and we will also implement methods to retrieve the data.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note that select Kotlin as the programming language.
Navigate to app > res > layout > activity_main.xml. Add the below code to your file. Below is the code for activity_main.xml.
activity_main.xml:
Design UI:
Navigate to app > kotlin+java > {package-name}, Right-click on it, New > Kotlin class and name it as DBHelper and add the below code to it. To make the code more understandable, comments are added.
DBHelper.kt:
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
MainActivity.kt:
Output: