Using SQLite database in Kotlin
SQLite is a relational database. Android comes with a built-in SQLite database. It is an open source SQL database and is widely used in Android apps. However, doing it in a raw manner is very time-consuming and eats up a lot of development and testing time. You have to work with cursors, iterate over them row by row, and wrap code in try-finally
, and such. Of course, you can use libraries that provide ORM mapping, which makes it easier to deal with a SQLite database, but if the database is small, it is expensive and is generally overkill. Kotlin, with Anko, provides a really easy way to deal with SQLite database. So let's get to work and see how we can use SQLite database in Kotlin.
Getting ready
We'll be using Android Studio 3.0 for coding. First, we need to add anko-sqlite to our build.gradle
file:
dependencies {
compile "org.jetbrains.anko:anko-sqlite:$anko_version"
}
You can replace $anko_version
with the latest version of the library.
How to do it…
Anko...