凉山州建设银行官方网站,百度下拉框推广网站,新手销售怎么和客户交流,西乡专业做网站公司目录 一、LitePal介绍
常用方法#xff1a;
1、插入数据#xff1a;
2、更新数据#xff1a;
3、删除数据#xff1a;
4、查询数据#xff1a;
二、LitePal的基本用法#xff1a;
1、集成LitePal#xff1a; 2、创建LitePal配置文件#xff1a;
3、创建模型类…目录 一、LitePal介绍
常用方法
1、插入数据
2、更新数据
3、删除数据
4、查询数据
二、LitePal的基本用法
1、集成LitePal 2、创建LitePal配置文件
3、创建模型类 4、增删改查操作
三、使用例子 MainActivity:
activity_main:
litepal.xml:
运行结果
四、异常修复 一、LitePal介绍 LitePal是一个开源的Android数据库框架它提供了简单易用的API来帮助开发者进行数据库操作。LitePal允许开发者使用面向对象的方式来操作数据库而无需编写复杂的SQL语句。
常用方法
1、插入数据
save()将当前模型对象保存到数据库中。saveAll(Collection models)将指定的模型对象集合保存到数据库中。2
2、更新数据
update()更新当前模型对象在数据库中的数据。updateAll(String... conditions)根据条件更新符合条件的数据。
3、删除数据
delete()删除当前模型对象在数据库中的数据。deleteAll(Class? modelClass, String... conditions)根据条件删除符合条件的数据。deleteAll(Class? modelClass)删除指定模型类的所有数据。
4、查询数据
find(Class? modelClass, long id)根据id查询指定模型类的数据。findFirst(Class? modelClass)查询指定模型类的第一条数据。findLast(Class? modelClass)查询指定模型类的最后一条数据。findAll(Class? modelClass)查询指定模型类的所有数据。where(String... conditions)设置查询条件。order(String... columns)设置查询结果的排序方式。limit(int limit)设置查询结果的数量限制。offset(int offset)设置查询结果的偏移量。average(Class? modelClass, String column)计算指定列的平均值。sum(Class? modelClass, String column)计算指定列的总和。max(Class? modelClass, String column)计算指定列的最大值。min(Class? modelClass, String column)计算指定列的最小值。
二、LitePal的基本用法
1、集成LitePal 首先在项目的build.gradle文件中添加LitePal的依赖
dependencies {implementation org.litepal.guolindev:core:版本号
}2、创建LitePal配置文件 在项目的assets目录下创建litepal.xml文件并配置数据库名称、版本号等信息。
?xml version1.0 encodingutf-8?
litepaldbname value数据库名称 /version value数据库版本号 /listmapping classcom.example.litepaltest.Book/mapping!-- 可以继续添加其他映射配置 --
/list
/litepal在AndroidManifest.xml中的代码中添加android:name org.litepal.LitePalApplication 3、创建模型类 创建与数据库表对应的模型类并继承自LitePalSupport。
import org.litepal.crud.LitePalSupport;public class Book extends LitePalSupport {private int id;private String name;private String author;// 省略getter和setter方法
}4、增删改查操作
插入数据
Book book new Book();
book.setName(Android入门);
book.setAuthor(张三);
book.save(); // 将数据保存到数据库中更新数据
Book book new Book();
book.setName(Android进阶);
book.updateAll(name ?, Android入门); // 将名称为Android入门的数据更新为Android进阶删除数据
LitePal.delete(Book.class, id); // 根据id删除指定的数据
LitePal.deleteAll(Book.class, name ?, Android入门); // 根据条件删除数据查询数据
ListBook bookList LitePal.findAll(Book.class); // 查询所有数据
Book book LitePal.findFirst(Book.class); // 查询第一条数据
ListBook bookList LitePal.where(author ?, 张三).find(Book.class); // 根据条件查询数据三、使用例子 MainActivity:
package com.example.litepaldemo;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.View;import org.litepal.FluentQuery;
import org.litepal.LitePal;import java.util.List;public class MainActivity extends AppCompatActivity {
String TAG MainActivity ;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void CreateDatabase(View view) {// 创建数据库LitePal.getDatabase();}public void addData(View view) {Book book new Book();book.setId(1);book.setAuthor(柏拉图);book.setName(理想国);book.setPages(259);book.setPrice(9.9);book.save();Book book1 new Book();book1.setId(2);book1.setAuthor(夸美纽斯);book1.setName(大教学论);book1.setPages(259);book1.setPrice(99.9);book1.save();}public void deleteData(View view) {
// LitePal.delete(Book.class, 1); // 根据id删除指定的数据LitePal.deleteAll(Book.class, name ?, Android进阶); // 根据条件删除数据}public void queryData(View view) {ListBook bookList LitePal.findAll(Book.class); // 查询所有数据for (Book book : bookList) {Log.d(TAG, 书名: book.getName());Log.d(TAG, 作者: book.getAuthor());Log.d(TAG, 页数: book.getPages());Log.d(TAG, 价格: book.getPrice());}}public void modifiedData(View view) {Book book new Book();book.setName(Android进阶);book.updateAll(name ?, 大教学论);}
}
activity_main:
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityButtonandroid:idid/create_databaseandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text创建数据库android:onClickCreateDatabaseapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintHorizontal_bias0.498app:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:layout_constraintVertical_bias0.13 /Buttonandroid:idid/add_dataandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text增加数据android:onClickaddDataapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintHorizontal_bias0.498app:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:layout_constraintVertical_bias0.278 /Buttonandroid:idid/del_dataandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text删除数据android:onClickdeleteDataapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintHorizontal_bias0.498app:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:layout_constraintVertical_bias0.395 /Buttonandroid:idid/query_dataandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text查询数据android:onClickqueryDataapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintHorizontal_bias0.498app:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:layout_constraintVertical_bias0.512 /Buttonandroid:idid/motified_dataandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text修改数据android:onClickmodifiedDataapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintHorizontal_bias0.498app:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:layout_constraintVertical_bias0.651 //androidx.constraintlayout.widget.ConstraintLayout
litepal.xml:
?xml version1.0 encodingutf-8?
litepaldbname valueBookStore /dbnameversion value1 /versionlistmapping classcom.example.litepaldemo.Book/mapping/list
/litepal
运行结果 四、异常修复
使用LItePal报错element ‘litepal‘ must be declared和Class referenced in the manifest, org.litepal.LitePa_敬往事一杯酒哈的博客-CSDN博客 文章转载自: http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.sjsfw.cn.gov.cn.sjsfw.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.lzwfg.cn.gov.cn.lzwfg.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.rbrd.cn.gov.cn.rbrd.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.skrh.cn.gov.cn.skrh.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.hkshy.cn.gov.cn.hkshy.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com