一、环境配置

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. 小米重新上锁[BL]

    解锁一时爽,bug火葬场.废话不多说,直接上教程. 首先安装 线刷工具:http://bigota.d.miui.com/tools/MiFlash2018-5-28-0.zip 解锁工具:http: ...

  2. React 函数生命周期

      React 函数生命周期基础 1 ,概念 在组件创建.到加载到页面上运行.以及组件被销毁的过程中,总是伴随着各种各样的事件,这些在组件特定时期,触发的事件,统称为组件的生命周期:* 2,组件生命周 ...

  3. 编写一个函数,输入n为偶数时,调用方法求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n

    需求:编写一个函数,输入n为偶数时,调用方法求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n package com.Summer_0511.cn; impo ...

  4. 非常NB的一款快捷启动软件--Merry

    Merry 被设计为了能将日常重复性操作简化为一个快捷键或者命令.Merry 采用完全开放的体系, 可以使用 Lua 或者外部程序来扩展 Merry 的功能. 另附一个自己扩展的LUA脚本: --启动 ...

  5. selenium 操作下拉处理

    操作下拉框处理 在网页中,有时候会遇到下拉框处理,这时候使用Webdriver提供的select类来处理. ##操作下拉框处理 #coding = utf-8 from selenium import ...

  6. CCF_ 201509-2_日期计算

    水. #include<iostream> #include<cstdio> using namespace std; ] = {{,,,,,,,,,,,,},{,,,,,,, ...

  7. 《Python学习手册 第五版》 -第6章 动态类型

    本章主要讲述变量.对象.引用三者直接的关联及区别,详细说明了在变量赋值的操作中,计算机内部到底发生了什么,有哪些是不被人察觉和需要明确了解的 1.先从最简单的赋值语句开始 a=3 这一句,基本就能涵盖 ...

  8. jsp关于request.setAttribue还有response.addCookie()的两个问题

    刚才使用request.getAttribute();时候无法获取到值,一直为null,经过分析,得到下面的 index1.jsp <%-- Created by IntelliJ IDEA. ...

  9. 根据navigator.userAgent返回值识别 浏览器

    function validBrowser(){ var u_agent = navigator.userAgent; var browser_name='Failed to identify the ...

  10. 【机器学习】算法原理详细推导与实现(六):k-means算法

    [机器学习]算法原理详细推导与实现(六):k-means算法 之前几个章节都是介绍有监督学习,这个章解介绍无监督学习,这是一个被称为k-means的聚类算法,也叫做k均值聚类算法. 聚类算法 在讲监督 ...