美文网首页
LitePal学习笔记

LitePal学习笔记

作者: Small_Cake | 来源:发表于2019-06-05 15:02 被阅读0次

原文链接:https://www.jianshu.com/p/dd88ea95de06

1.配置

1.1 添加依赖
//数据库
implementation 'org.litepal.android:core:2.0.0'
1.2 在assets中创建配置文件litepal.xml
<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="demo" />
    <version value="1" />
    <list>
        <mapping class="com.smallcake.SearchKey"/>
    </list>
</litepal>

注意:dbname是表名,version是版本号,list里面mapping映射到到对应的实体类,如果修改了对象中参数的名称或新增了参数,记得要修改版本号才会生效。

1.3 在自己的Application中初始化
 LitePal.initialize(MyApplication.this);

2.使用

2.1 增:保存数据

对应的实体类要保存,除了litepal.xml中要配置外,实体类还需要继承LitePalSupport

public class SearchKey extends LitePalSupport implements Serializable {
    private int time;
    private String key;
    public SearchKey(int time, String key) {
        this.time = time;
        this.key = key;
    }
}

然后保存该条数据

SearchKey searchKey = new SearchKey((int) (System.currentTimeMillis() / 1000), "牙刷");
searchKey.save();
2.2 删:删除数据
searchKey.delete();
2.3 查:查找出来的一般是数据集
List<SearchKey> all = LitePal.findAll(SearchKey.class);//查询出所有的SearchKey对象集合
List<SearchKey> searchKeys = LitePal.where("key=牙刷")
                .find(SearchKey.class);//查询出所有关键字是牙刷的对象集合
2.4 改:修改有多种方式
searchKey.setKey("毛巾");
searchKey.update(1);//把id为1的对象中的关键修改为毛巾

参考:
https://blog.csdn.net/guolin_blog/article/category/2522725

相关文章

网友评论

      本文标题:LitePal学习笔记

      本文链接:https://www.haomeiwen.com/subject/nlzjxctx.html