一、环境配置

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++;
}

【Android】LitePal的基础的更多相关文章

  1. Android 触摸手势基础 官方文档概览

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

  2. Android Content Provider基础

    Android Content Provider基础 Content Providers Content providers管理对一个结构化的数据集合的访问.它们封装了数据,并且提供了保护数据安全性的 ...

  3. Android 触摸手势基础 官方文档概览2

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

  4. Android应用开发基础篇(1)-----Button

    Android应用开发基础篇(1)-----Button   一.概述        Button,顾名思义就是按钮的意思,它主要的功能是响应用户按下按钮时的动作. 二.应用      新建一个工程, ...

  5. 2017 Android 面试题 [ 基础与细节 ]

    2017 Android 面试题 [ 基础与细节 ] 感谢@chuyao抛出的这些问题,平时业务代码写多了,很多基础的东西变得含糊不清了,这次裸辞出来找工作确实没有之前顺利,顺便求上海Android开 ...

  6. Android LitePal介绍与使用说明

    LitePal for Android LitePal是一个Android开源库,它使开发者使用SQLite数据库变得非常容易. 你可以不用写一句SQL语句就可以完成大部分数据库操作,包括创建表,更新 ...

  7. Android 的UI基础布局的学习

    一. 今天学习了Android 的UI基础布局的部分,绝大多数的布局都在Androidstudio的这个界面里,如下: 在左边的框里的palette的内部,包含了的大多数的布局所要用的button按钮 ...

  8. Android学习之基础知识九 — 数据存储(持久化技术)之使用LitePal操作数据库

    上一节学习了使用SQLiteDatabase来操作SQLite数据库的方法,接下来我们开始接触第一个开源库:LitePal.LitePal是一款开源的Android数据库框架,它采用了对象关系映射(O ...

  9. Android开发学习——基础学习

    在微信公众号上,发现一个自学android的一个文章,觉得不错.对其进行小小总结,整理给大家. 1. 基础UI学习 Button/TextView/EditText/CheckBox/ImageVie ...

随机推荐

  1. 计算机网络基础:TCP和UDP

    UDP(用户数据报协议) 应用场景:一个数据包就能完成数据通信:不需要建立会话和流量控制:多播.广播:是一种不可靠传输.(例如QQ聊天,屏幕广播) UDP协议特点: UDP是无连接的,即发送数据之前不 ...

  2. CassandraAppender - distributed logging,分布式软件logback-appender

    农历年最后一场scala-meetup听刘颖分享专业软件开发经验,大受启发.突然意识到一直以来都没有完全按照任何标准的开发规范做事.诚然,在做技术调研和学习的过程中不会对规范操作有什么严格要求,一旦技 ...

  3. JSP&Servlet学习笔记----第3章

    Web容器是JSP/Servlet唯一认识的HTTP服务器. HTTP是基于请求/响应的无状态通信协议. 流程: 1.请求来到HTTP服务器 2.HTTP服务器将请求转交给Web容器 3.Web容器创 ...

  4. windows 安裝 gcc 編譯器 - MinGW

    MinGW 介紹 MinGW 全稱 Minimalist GNU For Windows,是個精簡的Windows平台C/C++.ADA及Fortran編譯器,相比Cygwin而言,體積要小很多,使用 ...

  5. Codeforces_500_C

    http://codeforces.com/problemset/problem/500/C 数组从后向前代表当前书从上向下,当前位置只要向前找,找到当前位置的书,再将之间的重量加起来就可以了,相同的 ...

  6. 题解【[HNOI2010]弹飞绵羊】

    \[ \texttt{Description} \] 有 \(n\) 个弹力装置排成一排,第 \(i\) 个弹力装置的弹力系数是 \(k_i\) ,绵羊到第 \(i\) 个装置时,会被弹到第 \(i+ ...

  7. 利用视频解析网站免费观看各大平台VIP电影

    需求: 观看VIP电影.VIP电视 准备: 1.视频解析网站 2.VIP电影URL 教程开始: 1.百度搜索[视频解析],会索引出大量的视频解析网站,随便选择一个网站. 2.找到想观看的VIP视频,复 ...

  8. [教程分享]锐族MP3刷固件教程

    转载自我的博客:https://blog.ljyngup.com/archives/163.html/ (废话较多见谅) 在到学校前发现几个月前锐族mp3的固件更新了,赶紧刷了一个,发现网上关于这个售 ...

  9. php 上传文件 示例

    <?php header("content-type:text/html;charset=utf-8"); error_reporting(E_ALL); //session ...

  10. 使用helm安装jenkin和gitlab

    一.使用服务介绍 存储: 阿里云NAS k8s网络插件: calico k8s版本: 1.15.2 二.helm安装 https://www.cnblogs.com/zhangb8042/p/1020 ...