一、环境配置

LitePal 在GitHub地址为:https://github.com/LitePalFramework/LitePal

我们使用起来也很方便,直接在gradle中配置即可。

如果你是用的是Java版本,则在 build.gradle(Module:app)中添加:

dependencies {
implementation 'org.litepal.android:java:3.0.0'
}

Kotlin版本:

dependencies {
implementation 'org.litepal.android:kotlin:3.0.0'
}

然后点击Sync Now即可自动下载配置。

LitePal在使用的时候,需要配置一个litepal.xml文件放在安卓的assets目录中。

如果没有这个文件夹,点击你的项目app,右键new->Folder->Assets Folder

有了这个文件夹后,选中右键新建一个File,注意不要选择xml

命名为litepal.xml即可。

文件内容官方给的例子如下:

<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!--
Define the database name of your application.
By default each database name should be end with .db.
If you didn't name your database end with .db,
LitePal would plus the suffix automatically for you.
For example:
<dbname value="demo" />
-->
<dbname value="demo" /> <!--
Define the version of your database. Each time you want
to upgrade your database, the version tag would helps.
Modify the models you defined in the mapping tag, and just
make the version value plus one, the upgrade of database
will be processed automatically without concern.
For example:
<version value="1" />
-->
<version value="1" /> <!--
Define your models in the list with mapping tag, LitePal will
create tables for each mapping class. The supported fields
defined in models will be mapped into columns.
For example:
<list>
<mapping class="com.test.model.Reader" />
<mapping class="com.test.model.Magazine" />
</list>
-->
<list>
</list> <!--
Define where the .db file should be. "internal" means the .db file
will be stored in the database folder of internal storage which no
one can access. "external" means the .db file will be stored in the
path to the directory on the primary external storage device where
the application can place persistent files it owns which everyone
can access. "internal" will act as default.
For example:
<storage value="external" />
--> </litepal>

这样基础配置就完成了。

二、建表(Bean类)

在LitePal中,一个数据库的每一张表对应着一个类,这个类只需要继承自LitePalSupport

类中就会有save() 和 delete()两个方法,分别代表着保存(更新)和删除

我们需要创建一个Bean类,使用Alt+Insert快捷键可以快速调出生成菜单,生成get和set

例如:

import org.litepal.crud.LitePalSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 作者:created by 巴塞罗那的余晖 on 2019/10/23 15:16
* 邮箱:zhubaoluo@outlook.com
* 不会写BUG的程序猿不是好程序猿,嘤嘤嘤
*/
public class DiaryBean extends LitePalSupport {
String weather;
String author;
String content;
String dateString;
public DiaryBean(){
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
dateString=simpleDateFormat.format(date);
} public String getWeather() {
return weather;
} public void setWeather(String weather) {
this.weather = weather;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public String getDateString() {
return dateString;
} public void setDateString(String dateString) {
dateString = dateString;
}
}

在要放在数据库中的Bean类建好后,我们就需要去litepal.xml中去添加这个类。

<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="demo" />
<version value="1" />
<list>
<mapping class="com.paul.testlitepal.DiaryBean"/>
</list>
</litepal>

注意看一下这里的dbname即项目数据库名称,list中放的就是你需要使用数据库的类。

都是固定写法,请注意这里面的version,如果你要对list记录中进行删除或者添加操作,一定要将value的数字加一,否则不会生效且闪退。

三、使用

若要使用LitePal,则必须初始化。

即在onCreat中调用

LitePal.initialize(Context context);

最常用的方法是返回指定表中所有数据的集合:

List<DiaryBean> diaryBeans=LitePal.findAll(DiaryBean.class);

若未找到则会返回一个空的集合,注意一下。

更多的操作方法请参考官方文档

 LitePal.initialize(MainActivity.this);
LitePal.findFirst(DiaryBean.class);//查询DiaryBean表中的第一个元素
LitePal.findLast(DiaryBean.class);//查询DiaryBean表中的最后一个元素
LitePal.select("content","weather").find(DiaryBean.class);//查询相应字符的数据,其他是不会反回的
LitePal.order("dateString desc").find(DiaryBean.class);//根据dateString排序,desc降序,asc升序

对对象的操作请看代码:

LitePal.initialize(MainActivity.this);//初始化
/*-------创建一个DiaryBean对象并保存---------------*/
DiaryBean diary=new DiaryBean();
diary.setAuthor("小明");
diary.setWeather("晴天");
diary.setContent("啦啦啦我会用LitePal啦");
diary.save();
/*-------创建一个DiaryBean对象并保存---------------*/
List<DiaryBean> diaryBeans=LitePal.findAll(DiaryBean.class);//取出所有数据
int cont=0;
for(DiaryBean item:diaryBeans){
if(cont%2==0){
item.setAuthor("巴塞罗那的余晖");//修改数据
item.save();//调用save方法会自动更新
}else {
item.delete();//删除
}
       cont++;
}

最新文章

  1. KBEngine 学习笔记
  2. AIM Tech Round 3 (Div. 2) A B C D
  3. FQ 也要使用 Telegram
  4. 【转】移动端input输入placeholder垂直不居中
  5. Sublime Text3 激活教程
  6. UVa 10561 (SG函数 递推) Treblecross
  7. IOS开发之UIScrollView
  8. easyui获取日期datebox中的值
  9. Devstack: A copy of worked local.conf I&amp;#39;m sharing with you.
  10. NodeJS,我对“高、高、非”的一些看法
  11. 【Time系列五】个性时钟与秒表升级版
  12. 第二次项目冲刺(Beta阶段)--第七天
  13. linu下C语言之BMP图片操作编程(下)
  14. java的制作&quot;时间账本&quot;
  15. Android破解学习之路(十一)—— 关于去更新
  16. 2733: [HNOI2012]永无乡
  17. android基础----&gt;SQLite数据库的使用
  18. JNI学习笔记_Java调用C —— Android中使用的方法
  19. Appium学习路-安装篇
  20. ENVI数据格式

热门文章

  1. CVE-2020-3110、CVE-2020-3111、CVE-2020 -3118、CVE-2020-3119、CVE-2020-3120 cdpwn 解析
  2. MySql新版本安装配置
  3. Codeforces 1064D Labyrinth(双端队列BFS)
  4. 必知必会之Lambda表达式
  5. 《Redis5.x入门教程》正式推出
  6. Linux bash管道符“|”使用介绍与例子
  7. 编写windows服务程序
  8. 【问题】多重继承时,super函数只初始化继承的第一个类,不初始化第二个类。
  9. Windows2008R2搭建共享存储服务器
  10. img 标签上的src 链接图片不存在时 怎么处理