LitePal数据库使用方法


LitePal数据库使用方法

#在assets文件夹下创建litepal.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!--
Define the database name of your application.
By default each database name should be end with .db.
If you didn't name your database end with .db,
LitePal would plus the suffix automatically for you.
For example:
<dbname value="demo" />
-->
<dbname value="kotlin_demo" />

<!--
Define the version of your database. Each time you want
to upgrade your database, the version tag would helps.
Modify the models you defined in the mapping tag, and just
make the version value plus one, the upgrade of database
will be processed automatically without concern.
For example:
<version value="1" />
-->
<version value="1" />

<!--
Define your models in the list with mapping tag, LitePal will
create tables for each mapping class. The supported fields
defined in models will be mapped into columns.
For example:
<list>
<mapping class="com.test.model.Reader" />
<mapping class="com.test.model.Magazine" />
</list>
-->
<list>
<mapping class="com.sendinfo.kotlindemo.Video" />
</list>

<!--
Define where the .db file should be. "internal" means the .db file
will be stored in the database folder of internal storage which no
one can access. "external" means the .db file will be stored in the
path to the directory on the primary external storage device where
the application can place persistent files it owns which everyone
can access. "internal" will act as default.
For example:
<storage value="external" />
-->

</litepal>
说明:list节点下对应你的模型,会创建对应的表

app build.gradle

1
implementation 'org.litepal.android:kotlin:3.0.0'

Application初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
LitePal.initialize(this)
//监听数据库升级
LitePal.registerDatabaseListener(object :DatabaseListener{
override fun onCreate() {

L.d("数据库已经创建")
}

override fun onUpgrade(oldVersion: Int, newVersion: Int) {

}

})

模型类继承LitePalSupport

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
data class Video(

var comment: String,
var down: String,
var forward: String,
var header: String,
var images: Any,
var name: String,
var passtime: String,
var sid: String,
var text: String,
var thumbnail: String,
var top_comments_content: Any,
var top_comments_header: Any,
var top_comments_name: Any,
var top_comments_uid: Any,
var top_comments_voiceuri: Any,
var type: String,
var uid: String,
var up: String,
var video: String
):LitePalSupport()
说明:
1 id这个字段可写可不写,因为即使不写这个字段,LitePal也会在表中自动生成一个id列,毕竟每张表都一定要有主键的, 即不管实体类中有没有id这个属性,都会默认创建一个为整型的id字段,作为自增的主键。
id字段的值始终为当前记录的行号(下标从1开始),即使我们在实体类中定义了int或者long类型的id字段,在添加数据时人为的设置id的值为100,等其他值,查询数据库发现该id字段的值设置是无效的,她始终等于该条记录所在的行id,即第几条记录。

2 @Column(unique = true, defaultValue = "unknown") 是唯一的,且默认值为unknown
3 @Column(ignore = true) 忽略即是不在数据库中创建该属性对应的字段
4 @Column(nullable = false) 不为空

增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//模拟网络返回数据
val mm = JsonParser.fromJsonObj(HttpRequest.data,Result::class.javaObjectType)
var list = JsonParser.fromJsonArr(JsonParser.toJsonString(mm.result),
Video::class.java)


//插入
list.forEach { it

it.saveOrUpdate("uid=?",it.uid)
}

//查询所有
LitePal.findAll(Video::class.java).forEach { it

L.d(it.name)
}
//按照条件查询
val alist = LitePal.where("type = ?","video")
.order("uid")
.find<Video>()
L.json(alist)

更详细使用请点击这里