Skip to main content

SQLiter

Usage

Open a connection (file based DB)

// open a very basic connection
val config = DatabaseConfiguration("my-database.db", 1, { db -> ... })
val manager = createDatabaseManager(config)
val connection = manager.createMultiThreadedConnection()

// execute sql queries

// don't forget to close the connection
connection.close()

Open a connection (in-memory DB)

Why do in-memory databases need names?

If you pass in a name you need to manage the connections, just like any other database, but if you pass a value of null then SQLiter will auto-assign the name :memory: and the connection should auto-close when you disconnect.

// open a very basic connection
val config = DatabaseConfiguration(
name = null,
inMemory = true,
version = 1,
create = { db -> ... }
)
val manager = createDatabaseManager(config)
val connection = manager.createMultiThreadedConnection()

// execute sql queries

// don't forget to close the connection
connection.close()

Execute a query

Insert data
connection.withStatement("insert into test(num, str) values (?,?)") {
bindLong(1, id)
bindString(2, str)
executeInsert()
}
Query data
connection.withStatement("select * from test where num = ?") {
bindLong(1, id)
query().iterator().next().let {
val dbVal = it.values.get(1).second as String

...
}
}
Delete data
val statement = connection.createStatement("DELETE FROM test")
statement.execute()

// Verify that the deletion processed
assertEquals(0, connection.longForQuery("select count(*) from test"))
statement.finalizeStatement()