项目参考修改了https://github.com/senyuyuan/Gluttony 对存储,数据解析做了一定的改变
* 核心理念,对人类友好,KDao将会给您完美的使用体验
* 通过反射自动建表
* 实体类无需任何处理 (实现Serializable序列化)
//配置 数据库名称,cursorFactory,数据库版本
KDao.init(this, KDaoConfig("KDao_example", null, 1))
在Application或是首个Activity中,初始化KDao。
实体类无需做任何处理。 KDao在数据库中自动为您打理好一切。
注解:@PrimaryKey 用来指定主键。@Ignore用于忽略属性
data class UserData(
@PrimaryKey
var id: Int? = null,
var name: String = "",
var age: Int = -1,
var isChild: Boolean = false,
@Ignore
var thisIgnore: String = "thisIgnore"
) : Serializable
默认主键自增,主键字段可以为空 参数为是否替换主键相同的记录
val user = UserData()
user.id = 3
user.name = "sen"
user.age = 23
user.isChild = false
user.save(true)
//or
UserData(2, "john", 12, true).save()
val list = (1..1000).map { UserData(name = "批量$it", age = it, isChild = it % 2 == 0, thisIgnore = "这个是忽略的属性$it") }
list.saveAll(replace = true)
注:子对象暂时不支持查询(内部通过转换json实现)
School(name = "测试包含对象", student = (1..10).map { UserData(name = "批量$it", age = it, isChild = it % 2 == 0) }).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() // KDao 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: "KDao%" -> find values witch is starting with "KDao"
例如,"KDao%" -> 查询所有以"KDao"开头的数据