KQLite Database

KQLiteDatabase is an entry point to interact with database and excute queries. It requires KQLiteDriver to open and close underlying database connection. KQLite is independent of underlying driver implementation. Any SQLite driver that can open a compatible SQLiteConnection works.

It is recommened to use BundledSQLiteDriver for multiplatform projects.

Defining KQLiteDatabase

class ContactsDatabase(
    driver: KQLiteDriver = ContactsDbDriver(getDatabaseAbsolutePath(NAME))
) : KQLiteDatabase(driver) {
    companion object {
        const val NAME = "contacts.db"
        const val VERSION = 1
    }

    override fun getKQLiteTables(): List {
        return listOf(TblContact)
    }

}

Configuring PRAGMA settings

You can override onConfigure method in KQLiteDatabase to update PRAGMA settings. onConfigure is called when the database is opened, after the connection is established but before the schema is created, upgraded, or downgraded. It is invoked every time a database connection is opened except when the given version to KQLiteDatabase constructor is 0.

Common uses include enabling Write-Ahead Logging (WAL), setting the synchronous mode, or adjusting other performance-related settings.

override fun onConfigure(pragma: KQLitePragma) {
    pragma.foreignKeys.set(true)
    pragma.journalMode.set(JournalMode.WAL)
}

See KQLitePragma for all possible PRAGMA settings.

Database onCreate

onCreate is called when database schema is created for the very first time. It can be used to populate tables initially. Tables are created automatically before this callback.

override fun onCreate(connection: SQLiteConnection) {
    super.onCreate(connection)
    TblContact.quickInsert {
        it.firstName.bind("John")
        it.lastName.bind("Doe")
        it.phone.bind(JSON_ARRAY("1234567890"))
        it.type.bind(ContactType.Family)
        it.birthDate.bind(DATE("2000-01-01"))
    }
}

For more details and possibilities with KQLiteDatabase, please refer KQLiteDatabase Docs.

⬅ Drivers INSERT ⮕