码迷,mamicode.com
首页 > 数据库 > 详细

LitePal数据库框架的使用

时间:2015-06-27 18:04:42      阅读:642      评论:0      收藏:0      [点我收藏+]

标签:

    
配置:
  导入jar包
  在assets目录下新建litepal.xml,指定数据库名字和版本以及映射关系
<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="litepal_demo"></dbname>
    <version value="1"></version>
    <list>
        <mapping class="com.cbooy.litepal.domain.News"></mapping>
        <mapping class="com.cbooy.litepal.domain.Comment"></mapping>
        <mapping class="com.cbooy.litepal.domain.Category"></mapping>
        <mapping class="com.cbooy.litepal.domain.Introduction"></mapping>
    </list>
</litepal>
  在Application中配置,android:name="org.litepal.LitePalApplication"
 
 
初始化数据库
  Connector.getWritableDatabase(); 调用此方法即可完成数据库的创建
        
数据库的修改和升级:
     直接修改实体映射关系和字段,然后将数据库的version加1,就可以自动完成数据库的更新操作
 
关联关系:
     一对一,一对多,多对多,只要在实体中自然描述即可,映射关系会自动生成。
 
实体类的crud操作:
  保存:普通的实体类继承自 DataSupport即可完成save()保存一个,saveAll()保存一个集合的数据
  修改数据:
        修改一条数据,直接使用静态方法
    
DataSupport.update(Class<?> modelClass, ContentValues values, long id)
        修改多条,直接使用静态方法
    
DataSupport.updateAll(modelClass, values, conditions)
        conditions数组,由于它的类型是一个String数组,我们可以在这里填入任意多个String参数,其中最前面一个String参数用于指定约束条件,后面所有的String参数用于填充约束条件中的占位符(即?号),
        比如约束条件中有一个占位符,那么后面就应该填写一个参数,如果有两个占位符,后面就应该填写两个参数,以此类推
        例: DataSupport.updateAll(News.class, values, "title = ? and commentcount > ?", "xxxxx", "0"); 
        由于实体类继承了DataSupport,因此可以使用update方法直接修改
News updateNews = new News();  
updateNews.setTitle("new title");  
updateNews.update(2); 
        修改成默认值,比如说将评论数修改成0,只是调用updateNews.setCommentCount(0)这样是不能修改成功的,因为即使不调用这行代码,commentCount的值也默认是0。
            所以如果想要将某一列的数据修改成默认值的话,还需要借助setToDefault()方法。用法也很简单,在setToDefault()方法中传入要修改的字段名就可以了(类中的字段名)
News updateNews = new News();  
updateNews.setToDefault("commentCount");  
updateNews.updateAll();  
    删除数据:
        DataSupport.delete(News.class, 2);    删除一条数据
        DataSupport类中提供了一个isSaved()方法,这个方法返回true就表示该对象是经过持久化的,返回false则表示该对象未经过持久化。
 
    查询:
        通用的几个查询方式,使用id
  
News news = DataSupport.find(News.class, 1);
News firstNews = DataSupport.findFirst(News.class);
News lastNews = DataSupport.findLast(News.class);
List<News> newsList = DataSupport.findAll(News.class, 1, 3, 5, 7);
List<News> newsList = DataSupport.findAll(News.class, new long[] { 1, 3, 5, 7 });
List<News> allNews = DataSupport.findAll(News.class);

 

其他查询条件

List<News> news = DataSupport.where("id>?","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);   // limit
List<News> newsList = DataSupport.select("title", "content").where("commentcount > ?", "0").order("publishdate desc").limit(10).offset(10).find(News.class);  // 分页

 

如何查询关联表中的数据,每一个类型的find()方法,都对应了一个带有isEager参数的方法重载,设置成true就表示激进查询,这样就会把关联表中的数据一起查询出来了。 (不推荐)

最好的做法是在实体类中封装关联对象的查询操作。
public class News extends DataSupport{  
    public List<Comment> getComments() {  
        return DataSupport.where("news_id = ?", String.valueOf(id)).find(Comment.class);  
    }    
}

 

原生SQL查询支持:
Cursor cursor = DataSupport.findBySQL("select * from news where commentcount>?", "0"); 

 

聚合函数的支持,LitePal中一共提供了count()、sum()、average()、max()和min()这五种聚合函数

int result = DataSupport.count(News.class); 
int result = DataSupport.where("commentcount = ?", "0").count(News.class); 
int result = DataSupport.sum(News.class, "commentcount", int.class);  
double result = DataSupport.average(News.class, "commentcount");
int result = DataSupport.max(News.class, "commentcount", int.class);
int result = DataSupport.min(News.class, "commentcount", int.class);

 

LitePal数据库框架的使用

标签:

原文地址:http://www.cnblogs.com/cbooy/p/4604287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!