Skip to content
Android SQLite Kotlin Extensions
Kotlin
Branch: master
Clone or download
Latest commit 1d6a329 Dec 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
ask
buildSrc
docs Bumped to v0.5.1 Dec 14, 2019
gradle/wrapper
scripts Initial commit Oct 31, 2019
.gitignore Initial commit Oct 31, 2019
.travis.yml Initial commit Oct 31, 2019
LICENSE Initial commit Oct 31, 2019
README.md
build.gradle.kts Enabled JitPack Nov 23, 2019
gradle.properties Initial commit Oct 31, 2019
gradlew Updated Gradle to 6.0 Nov 16, 2019
gradlew.bat Initial commit Oct 31, 2019
settings.gradle.kts kae -> ask Nov 26, 2019

README.md

Build Status Coverage Status API GitHub license Android Arsenal JitPack

Android SQLite KTX

Kotlin extensions to simplify working with SQLite database.

Download

  • Gradle: Add the following to your build.gradle:
repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.github.xizzhu:ask:$latest_version'
}

Usage

Create and Drop Tables

  • To create a table, use the createTable() function:
database.createTable("tableName") {
  it["textColumn"] = TEXT + PRIMARY_KEY
  it["integerColumn"] = INTEGER + UNIQUE(ConflictClause.REPLACE)
  it["anotherTextColumn"] = TEXT + FOREIGN_KEY("referenceTable", "referenceColumn")
}

Each column must have one of the four types: BLOB, INTEGER, REAL, and TEXT. It can also also have one or more modifiers: PRIMARY_KEY, NOT_NULL, UNIQUE, DEFAULT, and FOREIGN_KEY.

More info can be found here.

  • To delete a table, use the dropTable() function:
database.dropTable("tableName")
  • To check if a table exists, use the hasTable() function:
database.hasTable("tableName")

Create and Drop Indices

  • To create an index, use the createIndex() function:
database.createIndex("indexName", "tableName", "column1", "column2")
  • To delete an index, use the dropIndex() function:
database.dropIndex("indexName")

Insert Values

To insert a row into a table, use the insert() function:

database.insert("tableName") {
  it["textColumn"] = "random text"
  it["integerColumn"] = 8964L
}

Update Values

To update an existing row, use the update() function:

database.update("tableName", { it["textColumn"] = "random new value" }) {
  ("integerColumn" eq 1L) and ("anotherTextColumn" eq "value")
}

It supports simple conditions like eq, less, etc., and logical conditions like and, or, etc. The full list of supported conditions can be found here.

Delete Values

  • To delete all values from a table, use the deleteAll() function:
database.deleteAll("tableName")
  • To delete values matching certain conditions, use the delete() function:
database.delete("tableName") {
  "integerColumn" eq 1L
}

It supports same conditions as discussed in the Update Values section.

Query Values

To query values from a table, use the select() function:

val query = database.select("tableName") {
  "integerColumn" eq 1L
}

It supports same conditions as discussed in the Update Values section.

The returned Query object can be further custmized by calling the groupBy(), limit() or other functions, e.g.:

query.groupBy("integerColumn")
  .having { max("integerColumn") greater 1L }

Note that the query is not executed, until asCursor() or one of the extension functions is called, e.g.:

// return a list of Pair<Integer, String>
query.toList { row ->
  row.getInt("integerColumn") to row.getString("textColumn")
}

// return the Integer value of integerColumn in the first row
query.first { row ->
  row.getInt("integerColumn")
}

More about Query can be found here.

Run a Transaction

  • To run a transaction, use the transaction() function:
database.transaction {
  // your transaction code
}

To abort the transaction, simply throw TransactionAbortedException.

  • To run a transaction with a return value, use the withTransaction() function:
val value = database.withTransaction {
  // your transaction code that returns a value
}

License

Copyright (C) 2019 Xizhi Zhu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.