Android数据库框架:LitePal例子展示
首先,想详细了解LitePal,请移步郭霖老师的专栏:Android数据库高手秘籍
本文,是我看完郭老师的博客,自己总结一遍,顺便写好代码,方便以后查看而已。
配置LitePal,请阅读Android数据库高手秘籍(二)——创建表和LitePal的基本用法
好了,直奔主题吧,直接看代码
工程目录如下:
litepal.xml文件
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="litepal" >
</dbname>
<version value="1" >
</version>
<list>
<mapping class="com.example.litepal.model.News" >
</mapping>
<mapping class="com.example.litepal.model.Comment" >
</mapping>
<mapping class="com.example.litepal.model.Introduction" >
</mapping>
<mapping class="com.example.litepal.model.Category" >
</mapping>
</list>
</litepal>
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.litepal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:name="org.litepal.LitePalApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.litepal.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
News.java文件
/**
* 新闻实体类(对数据的增删改查的操作都封装在DataSupport里,所以必须继承它)
* @author yongan.lin
*/
public class News extends DataSupport {
/** 新闻ID */
private int id;
/** 新闻的标题 */
private String title;
/** 新闻的内容 */
private String content;
/** 新闻的发布时间 */
private Date publishDate;
/** 新闻的评论数目 */
private int commentCount;
/** News和Introduction是一对一的关系 */
private Introduction introduction;
/** Comment和News是多对一的关系 */
private List<Comment> commentList = new ArrayList<Comment>();
/** Category和News是多对多的关系 */
private List<Category> categoryList = new ArrayList<Category>();
public News() {
super();
}
public News(String title, String content, Date publishDate, int commentCount) {
super();
this.title = title;
this.content = content;
this.publishDate = publishDate;
this.commentCount = commentCount;
}
此处省略get,set方法。。。
Introduction.java文件
/**
* 新闻简介的实体类
* @author yongan.lin
*/
public class Introduction extends DataSupport {
/** 新闻简介的id */
private int id;
/** 新闻简介的导语 */
private String guide;
/** 新闻简介的摘要 */
private String digest;
public Introduction(String guide, String digest) {
super();
this.guide = guide;
this.digest = digest;
}
此处省略get,set方法。。。
}
Comment.java文件
/**
* 评论实体类
* @author yongan.lin
*/
public class Comment extends DataSupport {
/** 评论的ID */
private int id;
/** 评论的内容 */
private String content;
/** 评论的发表时间 */
private Date publishDate;
/** 一条Comment对应一条News */
private News news;
public Comment() {
super();
}
public Comment(String content, Date publishDate) {
super();
this.content = content;
this.publishDate = publishDate;
}
此处省略get,set方法。。。
}
Category.java文件
/**
* 新闻类型的实体类
* @author yongan.lin
*/
public class Category extends DataSupport{
/** 新闻类型的id */
private int id;
/** 新闻类型的名字 */
private String name;
/** Category和News是多对多的关系 */
private List<News> newsList = new ArrayList<News>();
public Category(String name) {
super();
this.name = name;
}
此处省略get,set方法。。。
}
MainActivity.java文件(核心代码)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void insert() {
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();
// //save()方法还是有返回值的,我们可以根据返回值来判断存储是否成功
// if (news.save()) {
// Toast.makeText(MainActivity.this, "存储成功", Toast.LENGTH_SHORT).show();
// } else {
// Toast.makeText(MainActivity.this, "存储失败", Toast.LENGTH_SHORT).show();
// }
// //使用saveThrows()方法来存储数据,一旦存储失败就会抛出一个DataSupportException异常
// news.saveThrows();
// LitePal提供了一个saveAll()方法
List<News> newsList = new ArrayList<News>();
// saveAll()方法接收一个Collection集合参数,只要把待存储的集合数据传入即可.
DataSupport.saveAll(newsList);
}
private void delete() {
//删除news表中id为2的记录
DataSupport.delete(News.class, 2);
// 把news表中标题为“这是一条新闻标题”且评论数等于0的所有新闻都删除掉
DataSupport.deleteAll(News.class, "title = ? and commentcount = ?",
"这是一条新闻标题", "0");
//把news表中所有的数据全部删除掉
DataSupport.deleteAll(News.class);
//注意:一个非持久化的对象如果调用了delete()方法则不会产生任何效果
}
private void update(){
//把news表中id为2的记录的标题改成“这是二条新闻标题”
News updateNews = new News();
updateNews.setTitle("这是二条新闻标题");
updateNews.update(2);
//把news表中标题为“这是一条新闻标题”且评论数量大于0的所有新闻的标题改成“这是二条新闻标题”
News updateNews1 = new News();
updateNews.setTitle("这是二条新闻标题");
updateNews.updateAll("title = ? and commentcount > ?", "这是一条新闻标题", "0");
//把news表中所有新闻的评论数清零
News updateNews2 = new News();
updateNews.setToDefault("commentCount");
updateNews.updateAll();
//还有一种比较笨的方法就是先找出某条新闻,修改赋值,然后save()。
}
private void find() {
//查询news表中id为1的这条记录
News oneNews = DataSupport.find(News.class, 1);
// 查找出News表里的所有数据
List<News> allNews = DataSupport.findAll(News.class);
// 连缀查询,查询news表中所有评论数大于零的新闻
List<News> newsList = DataSupport.where("commentcount > ?", "0").find(
News.class);
// 只要title和content这两列数据,将查询出的新闻按照发布的时间倒序排列,只查询出前10条数据
List<News> newsList1 = DataSupport.select("title", "content")
.where("commentcount > ?", "0").order("publishdate desc")
.limit(10).find(News.class);
// 只要第11到第20条新闻,offset()方法,用于指定查询结果的偏移量
List<News> newsList2 = DataSupport.select("title", "content")
.where("commentcount > ?", "0").order("publishdate desc")
.limit(10).offset(10).find(News.class);
// 激进查询,会把关联表中的数据一起查询出来
List<News> news = DataSupport.findAll(News.class, true);
List<Comment> commentList = news.get(0).getCommentList();
// 原生查询
Cursor cursor = DataSupport.findBySQL(
"select * from news where commentcount>?", "0");
}
private void others() {
// 统计news表中一共有多少行数据
int result = DataSupport.count(News.class);
// 统计一共有多少条新闻是零评论的
int result1 = DataSupport.where("commentcount = ?", "0").count(
News.class);
// 要统计news表中评论的总数量(第二个参数是列名,表示我们希望对哪一个列中的数据进行求和,第三个参数用于指定结果的类型)
int allCommentcounts = DataSupport.sum(News.class, "commentcount",
int.class);
// 统计news表中平均每条新闻有多少评
double average = DataSupport.average(News.class, "commentcount");
// 想要知道news表中所有新闻里面最高的评论数是多少
int max = DataSupport.max(News.class, "commentcount", int.class);
// 想要知道news表中所有新闻里面最少的评论数是多少
int min = DataSupport.min(News.class, "commentcount", int.class);
}
}
好了,最后附上源码的下载地址:http://download.csdn.net/detail/linyongan/8800811