Skip to content

Sample app to demo prepopulation of Room database during build time

License

Notifications You must be signed in to change notification settings

rnpy/prepopulateDatabase

Repository files navigation

Room Database prepopulation during build

Sample app showing how to prepopulate a SQLite database during build and load it on Android (using Room).

More details to come later

buildSrc

Custom gradle task implemented in Kotlin to generate a SQLite database. It reads the schema definition generated by Room, creates a database file and inserts some data in it with current date (using xerial/sqlite-jdbc).

app

Demo Android app loading the prepopulated database.

Running the app will load the prepopulated database and display its content (10 entries containing their creation date). A very basic interface allows replacing that entire table content with new data tagged with current date. The code generating those queries is shared between app and buildSrc

The database generation task can be also executed independently using .\gradlew app:createDatabase, this should output:

> Task :app:createDatabase
Creating table Category
database updated
Category: {count(*)=10}

demolib

Both app and buildSrc depend on the same library. The entire code of the library is:

import java.util.*
const val TABLE = "Category"

data class SqlQuery(val query: String, val parameters: List<Array<out Any?>>)
class StaticDataInitializer {
    fun getData(count: Int): SqlQuery {
        val results = (1..count).mapTo(arrayListOf(), { arrayOf(it, "$it ${Date()}") })
        return SqlQuery("INSERT INTO $TABLE (id, title) VALUES (?,?)", results)
    }
}

demolib.jar contains the above code compiled using .\kotlinc demolib.kt -d demolib.jar

About

Sample app to demo prepopulation of Room database during build time

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages