Skip to content

Kotlin/Native interop to sqlite: a self-contained, embedded, and transactional SQL database engine

License

Notifications You must be signed in to change notification settings

Dominaezzz/kotlin-sqlite

Repository files navigation

kotlin-sqlite

Download

Kotlin/Native bindings to the sqlite C library.

SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private.

This library allows for 'kotlinified' use of SQLite and is based on the kotlinconf-spinner SQLite example. The aim is to eventually wrap all of SQLite in Kotlin (Native, JVM, etc).

Examples

withSqlite("dbPtr") { db ->
    println("SQLite Version: ${db.version}")

    db.execute("""
        CREATE TABLE LOL(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            age INTEGER NOT NULL
        );
        """)

    db.withStmt("INSERT INTO LOL(name, age) VALUES (?, ?);") { stmt ->
        stmt.bind(1, "Dominic")
        stmt.bind(2, 19)
        stmt.step()

        stmt.reset()

        stmt.bind(1, "Fischer")
        stmt.bind(2, 14)
        stmt.step()

        stmt.reset()

        stmt.bind(1, "Whatever")
        stmt.bind(2, -87)
        stmt.step()

        stmt.reset()

        stmt.bind(1, "Haha!")
        stmt.bind(2, 569)
        stmt.step()
    }

    db.execute("SELECT * FROM LOL;") { columns, data ->
        println("Columns: ${columns.joinToString()};   Data: {data.joinToString()}")
        0
    }

    db.withStmt("SELECT * FROM LOL;") { stmt ->
        while (stmt.step()) {
            println("id=${stmt.getColumnInt(0)}, name=${stmt.getColumnString(1)}, age=${stmt.getColumnLong(2)};")
        }
    }

    val count = db.withStmt("SELECT COUNT(*) FROM LOL;") { it.getColumnInt(0) }
}

Will have to look at the unit tests for more examples.

Notes

  • SQLite binaries are bundled with KLIBs.

  • The bindings are not final, nicely wrapping SQLite's C API is a WIP.

  • Contributions are welcome.

About

Kotlin/Native interop to sqlite: a self-contained, embedded, and transactional SQL database engine

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages