Gluttony, a super convenient and simple library to using Android database
饕餮,高效而简约地使用Android数据库
Library is compatible with Kotlin 1.0.5-2 build.
饕餮 兼容 kotlin 1.0.5-2 版本(最新版)
* The core concept, friendly to human, Gluttony will give you the perfect experience
* 核心理念,对人类友好,Gluttony将会给您完美的使用体验
* Automatic table and column naming through reflection
* 通过反射自动建表
* Entities do not need any processing
* 实体类无需任何处理
* The future will provide two kinds of the kernel for your choice,
【sqlite】 and 【realm】, the performance of the pursuit of perfection
* 未来将会提供两种内核供您选择,【sqlite】以及【realm】,追求极致的性能
as a gradle dependency
作为一个gradle依赖库
compile 'sen.yuan.dao:gluttony:1.1'
//configure databaseName,cursorFactory,databaseVersion
//配置 数据库名称,cursorFactory,数据库版本
Gluttony.init(this, GluttonyConfig("gluttony_example",null,1))
In Application or the first Activity, initialize Gluttony.
在Application或是首个Activity中,初始化Gluttony·饕餮。
Entities do not need to do any processing.
实体类无需做任何处理。
Gluttony automatically for you to take care of everything.
Gluttony·饕餮 在数据库中自动为您打理好一切。
Annotation: @PrimaryKey is used to specify a PrimaryKey.
注解:@PrimaryKey 用来指定主键。
data class UserData(
@PrimaryKey
var id: Int = -1,
var name: String = "",
var age: Int = -1,
var isChildren: Boolean = false
)
val user = UserData()
user.id = 666
user.name = "sen"
user.age = 23
user.isChildren = false
user.save()
//or
UserData(2, "john", 12, true).save()
val user1 = UserData().findOneByKey(666)
val user2 = UserData().findOne {
condition {
"age" between 7..16
"isChild" Not false
}
orderBy("age", SqlOrderDirection.ASC)
}
val userList = UserData().findAll {
condition {
"age" moreThan 11
"name" like "s%" //find names witch is starting with "s"
}
}
var user3 = UserData().findOne { condition { "name" equalsData "lucy" } }!!
user3.age += 1
user3.update()
//or
var user4 = UserData(7, "lucy", 10, true) //user4.id == user3.id · 注意primary key相同
user4.update() //user4 will overwrite the old data · 将会覆盖掉旧数据
var user5 = UserData(90, "white", 77, false) // 90 is a new primary key
user5.updateOrSave() // Gluttony will save a new data
//update user who id is 90 to named black,age 80
//lambda
UserData().updateByKey(90) { arrayOf("name" to "black", "age" to 80) }
//or pairs
UserData().updateByKey(90, "name" to "green", "age" to 82)
//update user who name is green to name red,age 99
UserData().update("name" to "red", "age" to 99) {
condition {
"name" equalsData "green"
}
}
var user9 = UserData(90) //only need primary key
user9.deleteAll()
UserData().deleteByKey(666) //deleteAll user which id is 666
//deleteAll users who is child
UserData().deleteAll {
condition {
"isChild" equalsData true
}
}
UserData().clear()
-
equalsData · 等于
-
moreThan · 大于
-
moreThanOrEquals · 大于等于
-
lessThan · 小于
-
lessThanOrEquals · 小于等于
-
Not · 非
-
In: Determine whether in the array · 判断是否在数组中
-
notIn: Determine whether not in the array
-
between: Determine whether in the range
-
like: fuzzy query · 模糊查询
two marks: % and _ · 两个通配符:% 和 _
% : indefinite amount content · 不定数量的内容
_ : one amount content · 一个位置的内容
for example: "gluttony%" -> find values witch is starting with "gluttony"
例如,"gluttony%" -> 查询所有以"gluttony"开头的数据