LitePal学习(五)——对boolean值的增删改查操作

前言

LitePal支持的数据类型有8种,分别为:
int、short、long、float、double、boolean、String和Date
然而在进行boolean类型操作的时候,还是有些问题的,下面就来具体讲讲。
本文参考以下链接:
Android数据库高手秘籍(二)——创建表和LitePal的基本用法
官网
今天要讲解的内容包括:

  • boolean的存储原理
  • boolean增删改查的具体操作
一.boolean的存储原理

对于LitePal数据库而言,存储boolean值为true的时候可以直接用LitePal存储对象的方式存储,若存储的对象中boolean为false,需要调用setToDefault(String name)方法存储,其中参数name为对象中Boolean属性的名称,为字符串
boolean值其实是以0和1的方式存到数据库中的,true=1,false=0,所以在查询的时候需要注意

下面以一个例子做讲解

二.先抛出数据表类Customer

当然,需要在litepal.xml中注册此model,然后视具体情况而定看是否需要升级数据库,下面是Customer类代码:

package com.android.model;

/**
 * Title:
 * Description:
 * <p>
 * Created by pei
 * Date: 2017/12/20
 */
public class Customer extends DataBaseModel{

    private String phoneNum;
    private boolean vip;

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public boolean isVip() {
        return vip;
    }

    public void setVip(boolean vip) {
        this.vip = vip;
    }
}

针对以上类做数据操作:

三.添加数据
    //添加新用户
    public void insertCustomer(Customer customer){
        boolean isVip=customer.isVip();
        if(!isVip){
            customer.setToDefault("vip");
        }
        customer.save();
    }

需要注意的是setToDefault(“vip”);中的”vip”为Customer中对应的boolean类型得属性名称,是字符串类型,在activity中调用如下:

  //添加一个Boolean为false的对象
                Customer customer=new Customer();
                customer.setPhoneNum("1238418");
                customer.setVip(false);
                insertCustomer(customer);

                //添加一个Boolean为true的对象
                Customer customer1=new Customer();
                customer1.setPhoneNum("7777");
                customer1.setVip(true);
                insertCustomer(customer1);
四.删除数据

删除电话号码为xxx的用户

 public void deleteCustomerByPhoneNum(String phoneNum){
        if(StringUtil.isNotEmpty(phoneNum)){
            DataSupport.deleteAll(Customer.class,"phoneNum=?",phoneNum);
        }
    }

activity中调用如下:

//删除
deleteCustomerByPhoneNum("13129953903");
五.更新数据

直接对boolean值做更改的方法如下:

   //更新,将所有用户设为vip/非vip
    public void changeVip(boolean isVip){
        ContentValues values=new ContentValues();
        values.put("vip",isVip);
        DataSupport.updateAll(Customer.class, values, "vip = ?", !isVip?"1":"0");
    }

在activity中调用如下:

                //更新,将所有用户设为vip/非vip
                changeVip(false);//将所有vip转成非vip
                changeVip(true);//将所有true非vip转成vip

根据电话号码修改boolean值

   //更新,将xxx电话号码的用户转成vip
    public void changeVipByPhoneNum(String phoneNum,boolean isVip){
        if(StringUtil.isNotEmpty(phoneNum)){
            ContentValues values=new ContentValues();
            values.put("vip",isVip);
            DataSupport.updateAll(Customer.class, values, "phoneNum = ?", phoneNum);
        }
    }

在activity中调用如下:

changeVipByPhoneNum("13129953903",true);
六.查询数据

查询所有:

   //查询所有
    public List<Customer> getAllCustomerList(){
        List<Customer> customerList = DataSupport.findAll(Customer.class);
        if(customerList==null){
            customerList=new ArrayList<>();
        }
        return customerList;
    }

activity中调用如下:

                //查询所有
                List<Customer>allList=getAllCustomerList();
                for (Customer c : allList) {
                    LogUtil.e(MainActivity.class, "====查询所有===c=" + c.toString());
                }

根据电话号码查boolean,代码如下:

    //根据电话号码查是否为vip
    public boolean whetherVipByphoneNum(String phoneNum){
        if(StringUtil.isNotEmpty(phoneNum)){
            List<Customer> customerList = DataSupport.where("phoneNum=?",phoneNum).find(Customer.class);
            if(CollectionUtil.isNotEmpty(customerList)){
                Customer customer=customerList.get(0);
                if(customer!=null){
                    return customer.isVip();
                }
            }
        }
        return false;
    }

activity中调用如下:

//根据电话号码查是否为vip
boolean isVip=whetherVipByphoneNum("13129953863");//13129953863
LogUtil.e(MainActivity.class, "====whetherVipByphoneNum===isVip=" + isVip);

根据boolean值查电话号码,代码如下:

    //查询 是/不是 vip的电话号码
    public List<Customer> getCustomerListByVip(boolean isVip){
        String vip=isVip?"1":"0";
        List<Customer> customerList = DataSupport.where("vip=?",vip).find(Customer.class);
        if(customerList==null){
            customerList=new ArrayList<>();
        }
        return customerList;
    }

在activity中调用如下:

                //查询 是vip的电话号码
                List<Customer>vipList=getCustomerListByVip(true);
                for (Customer c : vipList) {
                    LogUtil.e(MainActivity.class, "====是vip的电话号码===c=" + c.toString());
                }

                //查询 不是vip的电话号码
                List<Customer>notVipList=getCustomerListByVip(false);
                for (Customer c : notVipList) {
                    LogUtil.e(MainActivity.class, "====不是vip的电话号码===c=" + c.toString());
                }
七.注意事项
  • 当Customer需要存储flase时,需要调用setToDefault(” “)方法
  • 当boolean属性为查询条件时,不是直接以boolean值为true或false为查询条件,而是以字符串“0”或“1”为查询条件,例如下面:
public List<Customer> getCustomerListByVip(boolean isVip){
        String vip=isVip?"1":"0";
        List<Customer> customerList = DataSupport.where("vip=?",vip).find(Customer.class);
        if(customerList==null){
            customerList=new ArrayList<>();
        }
        return customerList;
    }
  • DataSupport.where(…),DataSupport.updateAll(…)和DataSupport.deleteAll(…)等方法里面涉及到的参数都是String,即意味着当你查询条件为boolean,int,long等非String类型参数时要转化成String参数进行查询,例如:
public List<Customer> getCustomerListByVip(boolean isVip){
        String vip=isVip?"1":"0";
        List<Customer> customerList = DataSupport.where("vip=?",vip).find(Customer.class);
        if(customerList==null){
            customerList=new ArrayList<>();
        }
        return customerList;
    }

以上方法中以boolean为查询条件时,vip并不是int类型的0或1,而是字符串类型

  • DataSupport.where(…)涉及到多个参数条件查询时可以用类似以下方式查询:
DataSupport.where(Person.class,"name=? and sex=?","小花","女");

ok,今天关于LitePal对于boolean的数据操作处理就讲到这里了,谢谢大家。

    原文作者:奔跑的佩恩
    原文地址: https://www.jianshu.com/p/98d3ee4af524
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞