栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Android数据库开发 LitePal的使用详解(2)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android数据库开发 LitePal的使用详解(2)

文章目录
    • @[TOC](文章目录)
  • 前言
  • 一、新增操作方法
    • 1.普通新增方法。
    • 2.抛出异常的存储方法。
    • 3.存储实体列表。
  • 二、删除操作方法
    • 1.普通删除方法
    • 2.根据ID删除
    • 3.批量删除数据
  • 三、修改数据操作方法
    • 1.根据ID删除
    • 2.批量更新数据
  • 四、查询操作方法
    • 1、简单查询
    • 2、连缀查询
    • 3、原生SQL查询
前言

前一篇文章介绍了LitePal的引入和基础配置,这一篇我们来介绍一下增删改查等操作,源码。


一、新增操作方法 1.普通新增方法。

代码如下(示例):

	UserEntity userEntity = new UserEntity();
 	userEntity.setAccount(account);
	userEntity.setPassword(password);
	userEntity.setName(name);
	userEntity.setPhone(phone);
	boolean result = userEntity.save();

新增方法很简单不需要使用SQLiteDatabase、ContentValues等,只需要new出一个对象,然后把要存储的数据通过setter方法传入,最后调用一下save()方法就可以。save()回返回一个boolean值,用于表示存储成功还是失败。

2.抛出异常的存储方法。

save()方法虽然可以根据返回值来判断是否存储成功,但是不会抛出失败的异常,如果我们要查找存储失败的异常,而不是返回一个false,那就可以使用saveThrows()方法来代替。代码如下:

	UserEntity userEntity = new UserEntity();
 	userEntity.setAccount(account);
	userEntity.setPassword(password);
	userEntity.setName(name);
	userEntity.setPhone(phone);
	userEntity.saveThrows();
3.存储实体列表。

上面的方法都是介绍的存储单个实体的方法。如果我们存储多个实体,可以使用 LitePal.saveAll()方法。
代码如下:

	List list= new ArrayList();
	UserEntity userEntity1 = new UserEntity();
 	userEntity1 .setAccount(“aaa”);
	userEntity1 .setPassword(“aaa”);
	userEntity1 .setName(“aaa”);
	userEntity1 .setPhone(“111111111”);

	UserEntity userEntity2 = new UserEntity();
 	userEntity2 .setAccount(“bbb”);
	userEntity2 .setPassword(“bbb”);
	userEntity2 .setName(“bbb”);
	userEntity2 .setPhone(“2222222”);
	
	UserEntity userEntity3 = new UserEntity();
 	userEntity3 .setAccount(“ccc”);
	userEntity3 .setPassword(“ccc”);
	userEntity3 .setName(“ccc”);
	userEntity3 .setPhone(“3333333”);
	
	list.add(userEntity1);
	list.add(userEntity2 );
	list.add(userEntity3 );
	boolean result =  LitePal.saveAll(list);
二、删除操作方法 1.普通删除方法

实体类不光有保存的save()方法也有删除的delete()方法,delete()方法的对象一定是要持久化之后的(即已保存到数据库),一个非持久化的对象如果调用了delete()方法则不会产生任何效果。这个delete()方法返回值是一个int值,即删除数据的条数,0表示删除了0条数据(删除失败)。代码如下:

	UserEntity userEntity = new UserEntity();
 	userEntity .setAccount(“ccc”);
	userEntity .setPassword(“ccc”);
	userEntity .setName(“ccc”);
	userEntity .setPhone(“3333333”);

	int result = userEntity.delete();
	//此时result 结果为0,因为这个实体还没有存储到数据库中

	userEntity.save();
	int result = userEntity.delete();
	//此时result 结果为1,表示删除了1条数据

实体类中isSaved()方法可以判断这个实体是否保存

if (userEntity.isSaved()) {
	userEntity.delete();
}
2.根据ID删除

LitePal.delete()方法可以根据ID来删除数据,这个方法有两个参数,第一个参数是Class,传入我们要删除的那个类的Class就好,第二个参数是一个指定的id,表示我们要删除的数据。
代码如下:

	//删除UserEntity表中 中 id为2的数据
   LitePal.delete(UserEntity.class,2);
3.批量删除数据

LitePal.deleteAll() 方法可以删除多条数据,这个方法有3个参数,第一个参数是Class,传入我们要删除的那个类的Class就好,第二个参数是判断条件
代码如下:

	//删除UserEntity表中 中 account =aaa 的数据
	 LitePal.deleteAll(UserEntity.class, "account = ?" , "aaa");
	//删除UserEntity表中 中 account =aaa且name = bbb 的数据
 	 LitePal.deleteAll(UserEntity.class, "account = ? and name = ? ", "aaa","bbb");

	//清空UserEntity表中数据
	LitePal.deleteAll(UserEntity.class);
三、修改数据操作方法 1.根据ID删除

根据ID修改更新数据也有两种方式一种是用实体类的更新方法,new出了一个实体对象,把要修改的数据直接set进去,最后调用一下update()方法并传入id就可以了,代码如下:

	//将UserEntity表中id为1的数据中的phone字段更新为1593267891
 	UserEntity userEntity =new UserEntity();
	userEntity.setPhone("1593267891");
	userEntity.update(1);

第二种方式是 LitePal.update()方法,这个方法有三个参数,第一个参数是Class,传入我们要修改的那个类的Class就好,第二个参数是ContentValues对象,这三个参数是一个指定的id,表示我们要修改哪一行数据,代码如下:

  	ContentValues values = new ContentValues();
	values.put("phone", "1593267891");
	LitePal.update(UserEntity.class,values,1);
2.批量更新数据

批量更新数据也有两种方式,第一种方式是用实体类的更新方法同样是new出了一个实体对象,把要修改的数据直接set进去,不过要调用updateAll()方法,如果没有参数就是更新全部,有参数的话就是根据条件进行更新
代码如下:

	UserEntity userEntity =new UserEntity();
	userEntity.setPhone("1593267891");
	//更新全部数据,将phone字段都改为1593267891
	userEntity.updateAll();
	
	//根据条件更新将account =aaa的数据的phone字段都改为1593267891
	userEntity.updateAll("account = ?" , "aaa");

第二种方式是 LitePal.updateAll()方法,代码如下:

	ContentValues values = new ContentValues();
	values.put("phone", "1593267891");
	//更新UserEntity实体的全部数据,将phone字段都改为1593267891
	LitePal.updateAll(UserEntity.class,values);

	//根据条件更新UserEntity实体z中account =aaa的数据的phone字段都改为1593267891
	LitePal.updateAll(UserEntity.class,values,"account = ?" , "aaa");
四、查询操作方法 1、简单查询

简单查询的操作方法有LitePal中的find()、findFirst()、findLast ()、findAll()这几个方法。
其中findFirst()、findLast ()是查询相应实体的第一个和最后一个数据,参数为相应的实体类。

	UserEntity userEntity1 = LitePal.findFirst(UserEntity.class);
	UserEntity userEntity2 = LitePal.findLast(UserEntity.class);

find()方法是根据实体类的ID进行查询。

	UserEntity userEntity3 =LitePal.find(UserEntity.class,2);

findAll()方法是根据实体类的条件进行查询

	//查询多个ID的数据
	List userEntityList1 = LitePal.findAll(UserEntity.class, 1, 3, 5, 7);

	long[] ids = new long[] { 1, 3, 5, 7 };
	List userEntityLis2 = LitePal.findAll(UserEntity.class, ids);

	//查询全部数据
	List userEntityLis3 = LitePal.findAll(UserEntity.class);
2、连缀查询

先调用LitePal的where()方法,在这里指定了查询条件。where()方法接收任意个字符串参数,其中第一个参数用于进行条件约束,从第二个参数开始,都是用于替换第一个参数中的占位符的。那这个where()方法就对应了一条SQL语句中的where部分。
接着我们在where()方法之后直接连缀了一个find()方法,然后在这里指定一个泛型类,表示用于查询哪张表。
代码如下:

	List userEntityLis4 = LitePal.where("account = ?", "aaa").find(UserEntity.class);

这个查询结果跟下面的sqL语句是相同的。

	select * from  userentity where account ="aaa";

同时还可以使用select()这个方法接收任意个字符串参数,每个参数要求对应一个列名,这样就只会把相应列的数据查询出来了。order()方法中接收一个字符串参数,用于指定查询出的结果按照哪一列进行排序,asc表示正序排序,desc表示倒序排序。limit()方法,这个方法接收一个整型参数,用于指定查询前几条数据,这里指定成10,意思就是查询所有匹配结果中的前10条数据。offset()方法,用于指定查询结果的偏移量,这里指定成10,就表示偏移十个位置,那么原来是查询前10条新闻的,偏移了十个位置之后,就变成了查询第11到第20条新闻了,如果偏移量是20,那就表示查询第21到第30条新闻,以此类推。
代码如下:

  List newsList = LitePal.select("account", "name")
                .where("account = ?", "aaa")
                .order("id desc").limit(10).offset(10)
                .find(UserEntity.class);

这个查询结果跟下面的sqL语句是相同的。

	select account,name from  userentity where account ="aaa" order by id desc limit 10,10;
3、原生SQL查询

findBySQL()方法,使用这个方法就能通过原生的SQL语句方式来查询数据,这个方法接收任意个字符串参数,其中第一个参数就是SQL语句,后面的参数都是用于替换SQL语句中的占位符的,用法非常简单。另外,findBySQL()方法返回的是一个Cursor对象,这和原生SQL语句的用法返回的结果也是相同的。

	 Cursor cursor = LitePal.findBySQL("select * from userentity where account =?", "aaa");
        int idIndex = cursor.getColumnIndex("id");
        int accountIndex = cursor.getColumnIndex("account");
        int passwordIndex = cursor.getColumnIndex("password");
        int nameIndex = cursor.getColumnIndex("name");
        int phoneIndex = cursor.getColumnIndex("phone");

        userList.clear();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(idIndex);
            String account = cursor.getString(accountIndex);
            String password = cursor.getString(passwordIndex);
            String name = cursor.getString(nameIndex);
            String phone = cursor.getString(phoneIndex);

            UserEntity userEntity = new UserEntity();
            userEntity.setId(id);
            userEntity.setAccount(account);
            userEntity.setPassword(password);
            userEntity.setName(name);
            userEntity.setPhone(phone);
            userList.add(userEntity);
        }
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1039904.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号