Kotlin x Anko (Part 2)

Budi Oktaviyan
2 min readMay 7, 2018

Hi, back again with story series of Anko 👍 … in the last article we already told about the basic of Anko. So, in this session I’ll continue explaining about how to create a simple SQLite DB using Anko.

First of all, we need to add dependencies… to enable Anko SQLite function;

dependencies {
implementation 'org.jetbrains.anko:anko-sqlite:0.10.5'
}

Usually we need to define SQLite helper to manage the creation of our database. In Anko, we also need to define that things… with simple way like this;

class DBOpenHelper(context: Context) : ManagedSQLiteOpenHelper(context, "user.db", null, 1) {override fun onCreate(db: SQLiteDatabase?) {
db?.createTable(
DatabaseHelper.TBL_USER,
true,
"id" to TEXT + PRIMARY_KEY + UNIQUE,
"name" to TEXT,
"age" to INTEGER,
"occupation" to TEXT)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db?.dropTable(
DatabaseHelper.TBL_USER,
true)
}
}

In above example, I also define the helper property for our table. It is just a singleton wrapper for our SQLite helper class;

object DatabaseHelper {  const val TBL_USER = "user"  fun getInstance(context: Context) = DBOpenHelper(context)
}

If you need any parcelable object to manipulate your model, you can create with your own way or using a kotlin parcelize instead 😃… Like most of mobile database implementation, if you need to do some query or writing transaction to DB… you have to run in your background thread as well. With Anko, we can do it easily like this way;

async(UI) {
bg {
database.use {
insert(DatabaseHelper.TBL_USER,
"name" to name,
"id" to id,
"age" to age,
"occupation" to occupation)
}
}
}

In the example, I’m defining my own extensions to getting the instance of SQLite DB object like this;

internal val Context.database: DBOpenHelper get() = DatabaseHelper.getInstance(this)

If you are asking a question about what is async and bg do in those example, it is the implementation of how kotlin coroutines with anko works … so, please stay tuned. Because I’ll explain it in the next part 😆

How about selecting a data in our SQLite using Anko ? basically, the way of doing that is not much different than writing data to SQLite. For example, we can define the selection of our data like this;

async(UI) {
bg {
database.use {
select(DatabaseHelper.TBL_USER).exec {
parseSingle(object : MapRowParser<User> {
override fun parseRow(columns: Map<String, Any?>): User {
val name = columns.getValue("name").toString()
val id = columns.getValue("id").toString()
val age = columns.getValue("age").toString().toInt()
val occupation =
columns.getValue("occupation").toString()
user = User(name, id, age, occupation)
return user ?: throw Exception("User not found!")
}
})
}
}
}.await()
}

That’s it for now… about how to create a simple SQLite DB using Anko 😃 … At the last part, I’ll explain to you what is coroutines in Kotlin and how Anko provide that with simple way. So, please don’t go anywhere guys 😳

--

--

Budi Oktaviyan

The only way to do great work, is to love what you do