Truth to be told: writing databases in Android is quite boring.

All the boilerplate required to work with SQLite is far from being the most delightful thing in the world.

Fortunately, there are already enough libraries that simplify this task, one of them announced during the latest Google I/O, called Room.

With Anko, however, we can continue to work as low-level as when we use the framework, but get rid of some of the more tedious parts of its implementation. Today we’ll see how.

Create your database with Anko

You’ll see here how to create your database from scratch. On Android, you’ll need a SQLiteOpenHelper, which you use to retrieve the database. You’ll then need to remember to close after the requests. Anko does this for you.

For this you need to include SQLite dependency for Anko:

Implement ManagedSQLiteOpenHelper

If you use this class, which extends SQLiteOpenHelper, you can create blocks of code where you will execute the database operations, as follows:

The content inside the braces is an extension function on the SQLiteDatabase class, so you can call its methods directly. With the added point that the table will be open just before and closed right after executing this block.

How do we implement this class? Based on Anko’s documentation, the recommended way is:

We create a small singleton that will save the instance of the helper, in a synchronized way so that different threads can not generate more than one instance.

In addition, we create an extension property for the context, so that any class that requires Context can access the database directly.

This allows us to make use of the initial code.

Define the table structure

To create our database, all we have to do is implement the onCreate method of the helper, and use the createTable extension function provided by Anko:

The first parameter is the name of the database, and the second parameter checks that the table doesn’t exist before doing the create.

The third is a vararg pair. That is, you can add as many as you like. As you can see, the pairs are created with the form A to B. This is called infix function, and is specified with the reserved word infix.

The second part of the pairs are constants of the SqlType class. I recommend that you take a look at the implementation, because it’s very interesting. Here it makes a clever use of the operator overloading.

Insert and query data

Everything becomes much easier with Anko. Instead of having to create your own ContentValue to add all the data during insertion, you can use an extension function on the database object, which is such that:

For queries, there are several ways to do them. In one of them you can add names to the variables of the query and include the values as pairs:

And there is another, more similar to that used in the Android framework, which allows to include interrogation sign and values more later. All of them must be String in this case:

Personally in this case I find the second option simpler.

You can also apply all regular database operations such as limit, orderBy, having or groupBy. You can see all the operations in Anko documentation.

To deal with the resulting cursor, Anko also provides us with different functions, such as parseSingle (for one result) or parseList (for several). These functions receive a rowParser.

There are a lot of different parsers. One interesting is the MapRowParser, which maps the columns to a map.

With this parser and map delegate, you can parse directly the values from a map to a class. You can see this explained in my book.


While it’s true that there are libraries that simplify much more the work with databases, for simple things Anko can be more than enough.

It saves us many of the things that make working with databases really painful, so it’s a good alternative for simple databases.

In addition, it shows us other ways to use language that can be very useful when you are facing other problems.

Don’t forget take a look at the previous articles to learn more about Kotlin!

Author: Antonio Leiva

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.