1. Using Anko SQLite in the project
Add anko-sqlite
dependencies to your build.gradle(Module app)
:
dependencies {
implementation "org.jetbrains.anko:anko-sqlite:0.10.8"
}
2. Access the database
Anko provides a ManagedSQLiteOpenHelper
special class that seamlessly replaces the default class. Here's how to use it:
class MyDatabaseOpenHelper private constructor(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "MyDatabase", null, 1) {
init {
instance = this
}
companion object {
private var instance: MyDatabaseOpenHelper? = null
@Synchronized
fun getInstance(ctx: Context) = instance ?: MyDatabaseOpenHelper(ctx.applicationContext)
}
override fun onCreate(db: SQLiteDatabase) {
// 在这里创建表
db.createTable("Customer", true,
"id" to INTEGER + PRIMARY_KEY + UNIQUE,
"name" to TEXT,
"photo" to BLOB)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
// 在这里,您可以像往常一样升级表
db.dropTable("User", true)
}
}
// Context的访问属性
val Context.database: MyDatabaseOpenHelper
get() = MyDatabaseOpenHelper.getInstance(this)
3. Create and delete tables
Using Anko, you can easily create new tables and delete existing tables. The syntax is very simple.
database.use {
createTable("Customer", true,
"id" to INTEGER + PRIMARY_KEY + UNIQUE,
"name" to TEXT,
"photo" to BLOB)
}
In SQLite, there are five main types: NULL
, INTEGER
, REAL
, TEXT
and BLOB
. But each column may have some modifiers like PRIMARY KEY
or . UNIQUE
You can append such modifiers and "add" them to the main type name.
To delete a table, use the following dropTable
function:
dropTable("User", true)
4. Insert data
// 其中db是SQLiteDatabase
// 例如:val db = database.writeableDatabase
db.insert("User",
"id" to 42,
"name" to "John",
"email" to "[email protected]"
)
Or database.use的
internally:
database.use {
insert("User",
"id" to 42,
"name" to "John",
"email" to "[email protected]"
)
}
Note that in the above example database
is a database helper instance and db
is an SQLiteDatabase
object
The function insertOrThrow()
, replace()
, replaceOrThrow()
also exists and has similar semantics.
5. Query data
Anko provides a convenient query builder. It can be created using an instance of db.select(tableName, vararg columns)
where .db
SQLiteDatabase
method | describe |
---|---|
column(String) |
Add a column to select query |
distinct(Boolean) |
Distinct query |
whereArgs(String) |
Specify raw String where query |
whereArgs(String, args) |
Specify a where query with arguments |
whereSimple(String, args) |
Specify a where query with ? mark arguments |
orderBy(String, [ASC/DESC]) |
Order by this column |
groupBy(String) |
Group by this column |
limit(count: Int) |
Limit query result row count |
limit(offset: Int, count: Int) |
Limit query result row count with an offset |
having(String) |
Specify raw having expression |
having(String, args) |
Specify a having expression with arguments |
Marked functions parse their arguments in a special way. They allow you to provide values in any order and support seamless escaping.
db.select("User", "name")
.whereArgs("(_id > {userId}) and (name = {userName})",
"userName" to "John",
"userId" to 42)