LitePal是一款开源的Android数据库框架,采用了ORM对象关系映射的模式,将常用的数据库功能进行了封装。(代码库)
基本用法
- 引入jar包
可以在这里下载LitePal的最新版本
也可以在github上下载LitePal的源码,使用Library的方式导入Eclipse中 -
配置litepal.xml
在assets
目录下建立litepal.xml文件
1
2
3
4
5
6
7
|
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="demo">
</dbname>
<version value="1">
</version>
<list>
</list>
</litepal>
|
dbname
用于设定数据库的名字,version
用于设定数据库版本号,list
用于设定所有的映射模型
-
配置LitePalApplication
在AndroidManifest.xml
中配置一个LitePalApplication
1
2
3
4
5
6
7
8
|
<manifest>
<application
name=
".LitePalApplication"
...
>
...
</application>
</manifest>
|
如果使用了自定义的Application继承LitePalApplication即可
建表
根据对象关系映射模式的概念,每一张表应该对应一个模型,比如对应的News模型
1
2
3
4
5
6
7
8
|
public
class News{
private
int id;
private String title;
private String content;
private Date publishDate;
private
int commentCount;
}
|
映射的数据类型一共有8种:int
、short
、long
、float
、double
、boolean
、String
和Date
使用LitePal只有声明private的字段才会被映射到数据表中,如果不想映射的话,修饰符设置为public
、protected
、default
就可以了
建立后再配置到映射表中,编辑asset目录下的litepal.xml的文件,在<list>
标签下加入News模型类的声明
1
2
3
4
5
6
7
8
|
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="demo">
</dbname>
<version value="1">
</version>
<list>
<mapping class="com.example.databasetest.model.News">
</mapping>
</list>
</litepal>
|
获取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
2
3
4
5
6
7
8
9
10
11
12
|
public
class Introduction {
private
int id;
private String guidie;
private String digest;
}
public
class Category {
private
int id;
private
int name;
}
|
在News
中添加Introduction
的引用
1
2
3
4
5
|
public
class News {
...
private Introduction introdution;
...
}
|
因为Comment
和News
是多对一的关系
1
2
3
4
5
6
7
8
9
10
11
|
public
class News {
...
private List<Comment> commentList =
new ArrayList<Comment>();
}
public
class Comment {
...
private News news;
...
}
|
对于news
和category
1
2
3
4
5
6
7
8
9
10
11
|
public
class News {
...
private List<Category> categoryList =
new ArrayList<Category>();
...
}
public
class Category {
...
private List<News> newsList =
new ArrayList<News>();
...
}
|
存储
要进行CRUD
操作所有实体类都需要继承自DataSupport
类
之后实体类就可以进行CRUD
操作了
1
2
3
4
5
|
News news =
new News();
news.setTitle(
"标题");
news.setContent(
"内容");
news.setPubllishDate(
new Date);
news.save();
|
也可以使用saveThrows()
方法,抛异常来捕捉存储失败的异常
其中LitePal
已经默默地帮我们设置了id
Comment
和News
是多对一的关系
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
Comment comment1 =
new Comment();
comment1.setContent(
"好评!");
comment1.setPublishDate(
new Date());
comment1.save();
Comment comment2 =
new Comment();
comment2.setContent(
"赞一个");
comment2.setPublishDate(
new Date());
comment2.save();
News news =
new News();
news.getCommentList().add(comment1);
news.getCommentList().add(comment2);
news.setTitle(
"第二条新闻标题");
news.setContent(
"第二条新闻内容");
news.setPublishDate(
new Date());
news.setCommentCount(news.getCommentList().size());
news.save();
|
这样多对一的关系就被建立,而且Comment
中的news_id
已经被默默赋值
如果有一个List<News> newsList
,可以使用DataSupport.saveAll(newsList)
来存储集合数据
修改数据
比如把news
表中id为2的记录的标题修改
1
2
3
|
ContentValues =
new ContentValues();
values.put(
'title',
"title修改");
DataSupport.update(News.class,values,
2);
|
修改多条数据
1
2
3
|
ContentValues values =
new ContentValues();
values.put(
"title",
"今日iPhone6 Plus发布");
DataSupport.updateAll(News.class, values,
"title = ?",
"今日iPhone6发布");
|
使用约束条件可以限定特定的数据,?
为占位符,用后面的String数据代入
比如:
1
2
3
|
ContentValues values =
new ContentValues();
values.put(
"title",
"今日iPhone6 Plus发布");
DataSupport.updateAll(News.class, values,
"title = ? and commentcount > ?",
"今日iPhone6发布",
"0");
|
如果全部修改可以不使用约束
1
2
3
|
ContentValues values =
new ContentValues();
values.put(
"title",
"今日iPhone6 Plus发布");
DataSupport.updateAll(News.class, values);
|
使用ContentValues
对象并不是那么友好,也可以直接使用实体类
1
2
3
4
5
6
7
|
News updateNews =
new News();
updateNews.setTtitle(
"新标题");
updateNews.update(
2);
News updateNews =
new News();
updateNews.setTitle(
"今日iPhone6发布");
updateNews.updateAll(
"title = ? and commentcount > ?",
"今日iPhone6发布",
"0");
|
如果要修改某列数据为默认值,使用setToDefault()
方法
删除
1
2
3
|
DataSupport.delete(News.class,
2);
DataSupport.deleteAll(News.class,
"title = ? and commentcount = ? ",
"title",
"0");
DataSupport.deleteAll(News.class);
|
查询
1
2
3
4
5
6
7
|
News news = DataSupport.find(News.class,
1);
News first = DataSupport.findFirst(News.class);
News last = DataSupport.findLast(News.class);
List<News> newsList = DataSupport.findAll(News.class,
1,
3,
5,
7);
long[]ids =
new
long[]{
1,
3,
5,
7};
List<News> newsList2 = DataSupport.findAll(News.class,ids);
List<News> newsList3 = DataSupport.findAll(News.class);
|
连缀查询
根据指定查询条件查询
1
2
3
4
5
6
7
8
9
10
11
12
13
|
List<News> newsList = DataSupport.where(
"commentcount > ?",
"0").find(News.class);
List<News> newsList = DataSupport.select(
"title",
"content")
.where(
"commentcount > ?",
"0").find(News.class);
List<News> newsList = DataSupport.select(
"title",
"content")
.where(
"commentcount > ?",
"0")
.order(
"publishdate desc").find(News.class);
List<News> newsList = DataSupport.select(
"title",
"content")
.where(
"commentcount > ?",
"0")
.order(
"publishdate desc").limit(
10).find(News.class);
List<News> newsList = DataSupport.select(
"title",
"content")
.where(
"commentcount > ?",
"0")
.order(
"publishdate desc").limit(
10).offset(
10)
.find(News.class);
|
激进查询
上述的查询无法查询关联表的数据,LitePal默认的模式就是懒查询,如果要一次性将关联表中的数据也一起查询出来,LitePal也支持激进查询的方式。
1
2
|
News news = DataSupport.find(News.class,
1,
true);
List<Comment> commentList = news.getCommentList();
|
使用懒加载也可以查询出关联数据,比如在News类中增加代码
1
2
3
4
5
6
|
public
class News extends DataSupport {
...
public List<Comment> getComments() {
return DataSupport.where(news_id=?),String.valueOf(id)).find(Comment.class);
}
}
|
原生查询
LitaPal也可以使用原生的查询(SQL语句)
1
|
Cursor cursor = DataSupport.findBySQL(
"select * from news where commentcount>?",
"0");
|
聚合函数
LitePal中提供了count()、sum()、average()、max()、min()这五种聚合函数