LitePal的使用
LitePal是一款开源的Android数据库框架,采用了ORM对象关系映射的模式,将常用的数据库功能进行了封装。
基本用法
- 引入jar包
可以在[这里](https://github.com/LitePalFramework/LitePal# latest-downloads)下载LitePal的最新版本
也可以在github上下载LitePal的源码,使用Library的方式导入Eclipse中 - 配置litepal.xml
在assets目录下建立litepal.xml文件1
2
3
4
5
6
7
<litepal>
<dbname value="demo"></dbname>
<version value="1"></version>
<list>
</list>
</litepal>dbname用于设定数据库的名字,version用于设定数据库版本号,list用于设定所有的映射模型 - 配置LitePalApplication
在AndroidManifest.xml中配置一个LitePalApplication如果使用了自定义的Application继承LitePalApplication即可1
2
3
4
5
6
7
8<manifest>
<application
name=".LitePalApplication"
...
>
...
</application>
</manifest>
建表
根据对象关系映射模式的概念,每一张表应该对应一个模型,比如对应的News模型
1 | public class News{ |
映射的数据类型一共有8种:int、short、long、float、double、boolean、String和Date
使用LitePal只有声明private的字段才会被映射到数据表中,如果不想映射的话,修饰符设置为public、protected、default就可以了
建立后再配置到映射表中,编辑asset目录下的litepal.xml的文件,在<list>标签下加入News模型类的声明
1 |
|
获取SQLiteDatabase的实例
1 | SQLiteDatabase db = Connector.getDatabase(); |
升级表
增加新的表或者改变表结构,只需要需要对应model的属性,在litepal.xml中进行配置,并对version进行加1处理
中级用法
建立表关联
- 一对一关系
比如相应News每一条新闻都有一段introduction简介
可以在introduction中设置一个news_id的外键列,存放具体新闻的id - 多对一关系
比如一个News对应多个Comment评论
在Comment中设置new_id列,存放具体New的ID,多个comment就对应了同一个New - 多对多
比如News可以有很多Category种类,Category对应很多News
可以新建一个中间表来存放news和category的关系
category_news表设置news_id和category_id两列,分别是news表的外键和category表的外键
使用LitePal建立关联表
新建Introduction和Category两个类
1 | public class Introduction { |
在News中添加Introduction的引用
1 | public class News { |
因为Comment和News是多对一的关系
1 | // news对应多个comment |
对于news和category
1 | public class News { |
存储
要进行CRUD操作所有实体类都需要继承自DataSupport类
之后实体类就可以进行CRUD操作了
1 | News news = new News(); |
也可以使用saveThrows()方法,抛异常来捕捉存储失败的异常
其中LitePal已经默默地帮我们设置了idComment和News是多对一的关系
1 | Comment comment1 = new Comment(); |
这样多对一的关系就被建立,而且Comment中的news_id已经被默默赋值
如果有一个List<News> newsList,可以使用DataSupport.saveAll(newsList)来存储集合数据
修改数据
比如把news表中id为2的记录的标题修改
1 | ContentValues = new ContentValues(); |
修改多条数据
1 | ContentValues values = new ContentValues(); |
使用约束条件可以限定特定的数据,?为占位符,用后面的String数据代入
比如:
1 | ContentValues values = new ContentValues(); |
如果全部修改可以不使用约束
1 | ContentValues values = new ContentValues(); |
使用ContentValues对象并不是那么友好,也可以直接使用实体类
1 | News updateNews = new News(); |
如果要修改某列数据为默认值,使用setToDefault()方法
删除
1 | DataSupport.delete(News.class,2); // 删除news表中id为2的记录,news_id为2的记录为外键的数据也会删除,返回值为被输出的记录数 |
查询
1 | News news = DataSupport.find(News.class,1); // 查询id为1的记录 |
连缀查询
根据指定查询条件查询
1 | List<News> newsList = DataSupport.where("commentcount > ?","0").find(News.class); |
激进查询
上述的查询无法查询关联表的数据,LitePal默认的模式就是懒查询,如果要一次性将关联表中的数据也一起查询出来,LitePal也支持激进查询的方式。
1 | News news = DataSupport.find(News.class,1,true); // true参数表示使用激进查询,关联的数据也会一起查出来 |
使用懒加载也可以查询出关联数据,比如在News类中增加代码
1 | public class News extends DataSupport { |
原生查询
LitaPal也可以使用原生的查询(SQL语句)
1 | Cursor cursor = DataSupport.findBySQL("select * from news where commentcount>?","0"); |
聚合函数
LitePal中提供了count()、sum()、average()、max()、min()这五种聚合函数
- count()
count()主要用于统计行数1
2int result = DataSupport.count(News.class);
int result = DataSupport.where("commnetcount = ?","0").count(News.class); - sum()
sum()用于对结果进行求和1
2
3// 第一个参数表示表,第二个参数是列名,第三个参数用于指定结果的类型
int result = DataSupport.sum(News.class,"commentcount",int.class);
// 表示所有的评论数 - average()
average()用于统计平均数1
double result = DataSupport.average(News.class,"commentcount");
- max()
求出列中最大的数值1
int result = DataSupport.max(News.class,"commentcount",int.class);
- min()
求出列中最小的数值1
int result = DataSupport.min(News.class,"commentcount",int.class);

