当前位置: 首页 > 工具软件 > LBase-Android > 使用案例 >

android LitePal数据库的使用

皇甫伟彦
2023-12-01

步骤

1.在build.gradle 中引入包

implementation 'org.litepal.android:core:1.6.1'

2.在应用的Application 中使用LitePalApplication

3.新建AirBean类

package com.maxus.smartscene.beans;

import org.litepal.crud.DataSupport;

public class AirBean extends DataSupport {
    private int airTemp;
    private int airFanLevel;
    private int airFanDirection;
    private String timeDate;
    private String modeName;

    public int getAirTemp() {
        return airTemp;
    }

    public void setAirTemp(int airTemp) {
        this.airTemp = airTemp;
    }

    public int getAirFanLevel() {
        return airFanLevel;
    }

    public void setAirFanLevel(int airFanLevel) {
        this.airFanLevel = airFanLevel;
    }

    public int getAirFanDirection() {
        return airFanDirection;
    }

    public void setAirFanDirection(int airFanDirection) {
        this.airFanDirection = airFanDirection;
    }

    public String getTimeDate() {
        return timeDate;
    }

    public void setTimeDate(String timeDate) {
        this.timeDate = timeDate;
    }

    public String getModeName() {
        return modeName;
    }

    public void setModeName(String modeName) {
        this.modeName = modeName;
    }

    @Override
    public String toString() {
        return "AirBean{" +
                "airTemp=" + airTemp +
                ", airFanLevel=" + airFanLevel +
                ", airFanDirection=" + airFanDirection +
                ", timeDate='" + timeDate + '\'' +
                ", modeName='" + modeName + '\'' +
                '}';
    }
}

4.在assets中建立litepal.xml,内容如下(AirBean的路径就是你需要操作数据实体类的全路径,上面黄色部分)

<?xml version="1.0" encoding="utf-8" ?>
<litepal>
    <dbname value="airBean"></dbname>
    <version value="1"></version>
    <list>
        <mapping class="com.maxus.smartscene.beans.AirBean"></mapping>
    </list>
</litepal>

5.初始化

LitePal.getDatabase();//相当于调用onCreate()或者onUpgrade()

6.相关使用方法

private void addBean() {
//        AirBean airBean = new AirBean();
//        airBean.setAirTemp(airTemp++);
//        airBean.setAirFanLevel(3);
//        airBean.setAirFanDirection(4);
//        airBean.save();
//
//    }
//
//    private void modifyBean() {
//        AirBean airBean = new AirBean();
//        airBean.setAirTemp(18);
//        airBean.saveOrUpdate("airTemp=?", "28");
//        // airBean.updateAll("airTemp=? and author=?","Android Studying","G");
//    }
//
//    private void deleteBean() {
//        DataSupport.deleteAll(AirBean.class, "airTemp<?", "19");
//    }
//
//    private void selectBean() {
//
//        List<AirBean>airBeans=DataSupport.where("airTemp>?","20").find(AirBean.class);
//
//        //List<AirBean> airBeans=DataSupport.select("airTemp=15").find(AirBean.class);
//        Log.d("AirBean", "size===" + airBeans.size());
//        for (int i = 0; i <airBeans.size() ; i++) {
//            Log.d("airBean", airBeans.get(i).toString()+"\n");
//        }
//

 类似资料: