Skip to content

Instantly share code, notes, and snippets.

@nosix nosix/Client.kt
Last active Aug 25, 2017

Embed
What would you like to do?
Usage of SQLite for Android (API Level 21) in Kotlin 1.0.4
package xxx
import android.content.Context
import xxx.Database
import xxx.SampleDB
class Client(context: Context) {
private val db = Database(context, SampleDB())
fun save(time: Int, type: Int) {
db.update {
val seqId = it.sampleTable.nextSeqId()
it.sampleTable.insert(seqId, time, type)
}
}
}
package xxx
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class Database<out D : Database.Dao>(context: Context, val def: Definition<D>) {
interface Dao {
fun onCreate()
}
abstract class Definition<out D : Dao> {
abstract val name: String
abstract val version: Int
val cursorFactory: SQLiteDatabase.CursorFactory? = null
abstract fun create(db: SQLiteDatabase): D
}
private val helper = object : SQLiteOpenHelper(context, def.name, def.cursorFactory, def.version) {
override fun onCreate(db: SQLiteDatabase) {
def.create(db).onCreate()
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}
}
fun <R> query(procedure: (D) -> R): R {
helper.readableDatabase.use {
return procedure(def.create(it))
}
}
fun <R> update(procedure: (D) -> R): R {
helper.writableDatabase.use {
it.beginTransaction()
try {
val result = procedure(def.create(it))
it.setTransactionSuccessful()
return result
}
finally {
it.endTransaction()
}
}
}
}
package xxx
import android.database.sqlite.SQLiteDatabase
import xxx.dao.SampleTableDao
class SampleDB : Database.Definition<SampleDB.Dao>() {
override val name = "sample.db"
override val version = 1
class Dao(db: SQLiteDatabase) : Database.Dao {
val sampleTable = SampleTableDao(db)
override fun onCreate() {
sampleTable.createTable()
}
}
override fun create(db: SQLiteDatabase) = SampleDB.Dao(db)
}
package xxx.dao
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import xxx.insert
import xxx.query
class SampleTableDao(val db: SQLiteDatabase) {
companion object {
private val TABLE_NAME = "sample_table"
}
fun createTable() {
db.execSQL("""
CREATE TABLE $TABLE_NAME(
seq_id INTEGER,
time INTEGER,
type INTEGER
PRIMARY KEY(seq_id, time)
)
""")
}
fun nextSeqId(): Int {
db.query(TABLE_NAME, arrayOf("max(seq_id)")).use {
return 1 + if (it.moveToNext()) it.getInt(0) else 0
}
}
fun insert(seqId: Int, time: Int, type: Int) {
val values = ContentValues().apply {
put("seq_id", seqId)
put("time", time)
put("type", type)
}
db.insert(TABLE_NAME, values)
}
}
package xxx
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
fun SQLiteDatabase.query(
table: String,
columns: Array<out String>,
selection: String? = null,
selectionArgs: Array<out String>? = null,
groupBy: String? = null,
having: String? = null,
orderBy: String? = null,
limit: String? = null,
distinct: Boolean = false) =
query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)
fun SQLiteDatabase.insert(
table: String,
values: ContentValues,
nullColumnHack: String? = null) =
insert(table, nullColumnHack, values)
@heptorsj

This comment has been minimized.

Copy link

commented Aug 25, 2017

Great Example, many thanks is just what i needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.