一、建表:
①1. 引入Jar包或源码,guthub地址
② 配置litepal.xml
在项目的assets目录(和Java,res同级别的文件夹)下面新建一个litepal.xml文件,并将以下代码拷贝进去:
<?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> <mapping class="com.example.databasetest.model.Comment"></mapping> </list> </litepal><dbname>用于设定数据库的名字,<version>用于设定数据库的版本号,<list>用于设定所有的映射模型,一定要填入类的完整类名。
③配置LitePalApplication: 在application里初始化
//初始化 LitePal.initialize(this); //此初始化可替换成别的配置方式,具体见github //需要操作一下数据库,表自动生成 SQLiteDatabase db = Connector.getDatabase();二、升级表
①按照需求,增加或修改对应的实体类(model)
②litepal.xml版本号加1
三、 建立表关联
在一个类里,有别的类的实例,由此将表关联起来
public class News extends DataSupport{ ... private Introduction introduction; private List<Comment> commentList = new ArrayList<Comment>(); // 自动生成get、set方法 } public class Comment extends DataSupport{ ... private News news; // 自动生成get、set方法 }四、存储数据
demo1:无需引用别的类
News news = new News(); news.setTitle("这是一条新闻标题"); news.setContent("这是一条新闻内容"); news.setPublishDate(new Date()); news.save();判断存储成功或失败:
if (news.save()) { Toast.makeText(context, "存储成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "存储失败", Toast.LENGTH_SHORT).show(); }demo2:需引用别的类
//当News里有list<Comment>实例时,需先保存Comment,再保存News Comment comment1 = new Comment(); comment1.setContent("好评!"); comment1.save(); Comment comment2 = new Comment(); comment2.setContent("赞一个"); comment2.save(); News news = new News(); news.getCommentList().add(comment1); news.getCommentList().add(comment2); news.save();小技巧:保存List<News> newsList
demo:
DataSupport.saveAll(newsList);五、修改数据
demo1:指定修改特定id
News updateNews = new News(); updateNews.setTitle("今日iPhone6发布"); updateNews.update(2);demo2:筛选修改
News updateNews = new News(); updateNews.setTitle("今日iPhone6发布"); updateNews.updateAll("title = ? and commentcount > ?", "今日iPhone6发布", "0");demo3:恢复默认值
News updateNews = new News(); updateNews.setToDefault("commentCount"); updateNews.updateAll();六、删除数据
demo1:删除news表中id为2的记录
DataSupport.delete(News.class, 2);需要注意的是,这不仅仅会将news表中id为2的记录删除,同时还会将其它表中以news id为2的这条记录作为外键的数据一起删除掉。
demo2:筛选删除
DataSupport.deleteAll(News.class, "title = ? and commentcount = ?", "今日iPhone6发布", "0");demo3:全部删除
DataSupport.deleteAll(News.class);七、查询数据
demo1:查询news表中id为1的这条记录
News news = DataSupport.find(News.class, 1);demo2:取出表中的第一条数据
// News firstNews = DataSupport.findFirst(News.class); // 筛选查询 News firstNews = DataSupport.where("commentcount > ?", "0").findFirst(News.class);demo3:查询news表中id为1,3,5,7的记录
// List<News> newsList = DataSupport.findAll(News.class, 1, 3, 5, 7); long[] ids = new long[] { 1, 3, 5, 7 }; List<News> newsList = DataSupport.findAll(News.class, ids);demo4:查询所有数据
List<News> allNews = DataSupport.findAll(News.class);小技巧:连缀查询
demo1:查询news表中所有评论数大于零的新闻
List<News> newsList = DataSupport.where("commentcount > ?", "0").find(News.class);demo2:只要title和content这两列数据
List<News> newsList = DataSupport.select("title", "content") .where("commentcount > ?", "0").find(News.class);demo3:将查询出的新闻按照发布的时间倒序排列
List<News> newsList = DataSupport.select("title", "content") .where("commentcount > ?", "0") .order("publishdate desc").find(News.class);order()方法中接收一个字符串参数,用于指定查询出的结果按照哪一列进行排序,asc表示正序排序,desc表示倒序排序
demo4:只查询出前10条数据
List<News> newsList = DataSupport.select("title", "content") .where("commentcount > ?", "0") .order("publishdate desc").limit(10).find(News.class);demo5:对新闻进行分页展示,翻到第二页时,展示第11到第20条新闻
List<News> newsList = DataSupport.select("title", "content") .where("commentcount > ?", "0") .order("publishdate desc").limit(10).offset(10) .find(News.class);offset()方法,用于指定查询结果的偏移量,这里指定成10,就表示偏移十个位置
小技巧:激进查询
上述我们的所有用法中,都只能是查询到指定表中的数据而已,关联表中数据是无法查到的,因为LitePal默认的模式就是懒查询
demo1:
News news = DataSupport.find(News.class, 1, true); List<Comment> commentList = news.getCommentList();每一个类型的find()方法,都对应了一个带有isEager参数的方法重载,设置成true就表示激进查询,这样就会把关联表中的数据一起查询出来了。
demo2:
public class News extends DataSupport{ ... public List<Comment> getComments() { return DataSupport.where("news_id = ?", String.valueOf(id)).find(Comment.class); } }小技巧:原生查询
demo:
Cursor cursor = DataSupport.findBySQL("select * from news where commentcount>?", "0");八、聚合函数
①行数 count()
// int result = DataSupport.count(News.class); //全部行数 int result = DataSupport.where("commentcount = ?", "0").count(News.class); //评论为0的行数②sum():统计news表中评论的总数量
int result = DataSupport.sum(News.class, "commentcount", int.class);sum()方法只能对具有运算能力的列进行求合,比如说整型列或者浮点型列,如果你传入一个字符串类型的列去求合,肯定是得不到任何结果的,这时只会返回一个0作为结果; 以下方法也是如此
③average():统计news表中平均每条新闻有多少评论
double result = DataSupport.average(News.class, "commentcount");第一个参数是Class。第二个参数用于指定列名的,表示我们想要统计哪一列的平均数。
④max():某个列中最大的数值
demo:news表中所有新闻里面最高的评论数是多少
int result = DataSupport.max(News.class, "commentcount", int.class);第一个参数Class,第二个参数是列名第三个参数用于指定结果的类型。
五:min():某个列中最小的数值
demo:news表中所有新闻里面最少的评论数是多少
int result = DataSupport.min(News.class, "commentcount", int.class);来源:摘自郭大神博客:http://blog.csdn.net/guolin_blog/article/details/40614197