目錄
一、LitePal介紹
常用方法:
1、插入數(shù)據(jù):
2、更新數(shù)據(jù):
3、刪除數(shù)據(jù):
4、查詢數(shù)據(jù):
二、LitePal的基本用法:?
1、集成LitePal:
?2、創(chuàng)建LitePal配置文件:
3、創(chuàng)建模型類:
?4、增刪改查操作:
三、使用例子?
?MainActivity:
activity_main:
litepal.xml:?
運行結(jié)果:?
四、異常修復(fù)
一、LitePal介紹
????????LitePal是一個開源的Android數(shù)據(jù)庫框架,它提供了簡單易用的API來幫助開發(fā)者進行數(shù)據(jù)庫操作。LitePal允許開發(fā)者使用面向?qū)ο蟮姆绞絹聿僮鲾?shù)據(jù)庫,而無需編寫復(fù)雜的SQL語句。
常用方法:
1、插入數(shù)據(jù):
-
save()
:將當前模型對象保存到數(shù)據(jù)庫中。 -
saveAll(Collection models)
:將指定的模型對象集合保存到數(shù)據(jù)庫中。2
2、更新數(shù)據(jù):
-
update()
:更新當前模型對象在數(shù)據(jù)庫中的數(shù)據(jù)。 -
updateAll(String... conditions)
:根據(jù)條件更新符合條件的數(shù)據(jù)。
3、刪除數(shù)據(jù):
-
delete()
:刪除當前模型對象在數(shù)據(jù)庫中的數(shù)據(jù)。 -
deleteAll(Class<?> modelClass, String... conditions)
:根據(jù)條件刪除符合條件的數(shù)據(jù)。 -
deleteAll(Class<?> modelClass)
:刪除指定模型類的所有數(shù)據(jù)。
4、查詢數(shù)據(jù):
-
find(Class<?> modelClass, long id)
:根據(jù)id查詢指定模型類的數(shù)據(jù)。 -
findFirst(Class<?> modelClass)
:查詢指定模型類的第一條數(shù)據(jù)。 -
findLast(Class<?> modelClass)
:查詢指定模型類的最后一條數(shù)據(jù)。 -
findAll(Class<?> modelClass)
:查詢指定模型類的所有數(shù)據(jù)。 -
where(String... conditions)
:設(shè)置查詢條件。 -
order(String... columns)
:設(shè)置查詢結(jié)果的排序方式。 -
limit(int limit)
:設(shè)置查詢結(jié)果的數(shù)量限制。 -
offset(int offset)
:設(shè)置查詢結(jié)果的偏移量。 -
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、創(chuàng)建LitePal配置文件:
????????在項目的assets
目錄下創(chuàng)建litepal.xml文件,并配置數(shù)據(jù)庫名稱、版本號等信息。
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="數(shù)據(jù)庫名稱" />
<version value="數(shù)據(jù)庫版本號" />
<list>
<mapping class="com.example.litepaltest.Book"></mapping>
<!-- 可以繼續(xù)添加其他映射配置 -->
</list>
</litepal>
? ? ? ? 在AndroidManifest.xml中的代碼中,添加android:name = "org.litepal.LitePalApplication"
3、創(chuàng)建模型類:
????????創(chuàng)建與數(shù)據(jù)庫表對應(yīng)的模型類,并繼承自LitePalSupport。文章來源:http://www.zghlxwxcb.cn/news/detail-637359.html
import org.litepal.crud.LitePalSupport;
public class Book extends LitePalSupport {
private int id;
private String name;
private String author;
// 省略getter和setter方法
}
?4、增刪改查操作:
- 插入數(shù)據(jù):
Book book = new Book();
book.setName("Android入門");
book.setAuthor("張三");
book.save(); // 將數(shù)據(jù)保存到數(shù)據(jù)庫中
- 更新數(shù)據(jù):
Book book = new Book();
book.setName("Android進階");
book.updateAll("name = ?", "Android入門"); // 將名稱為"Android入門"的數(shù)據(jù)更新為"Android進階"
- 刪除數(shù)據(jù):
LitePal.delete(Book.class, id); // 根據(jù)id刪除指定的數(shù)據(jù)
LitePal.deleteAll(Book.class, "name = ?", "Android入門"); // 根據(jù)條件刪除數(shù)據(jù)
- 查詢數(shù)據(jù):
List<Book> bookList = LitePal.findAll(Book.class); // 查詢所有數(shù)據(jù)
Book book = LitePal.findFirst(Book.class); // 查詢第一條數(shù)據(jù)
List<Book> bookList = LitePal.where("author = ?", "張三").find(Book.class); // 根據(jù)條件查詢數(shù)據(jù)
三、使用例子?
?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" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void CreateDatabase(View view) {
// 創(chuàng)建數(shù)據(jù)庫
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("大教學(xué)論");
book1.setPages(259);
book1.setPrice(99.9);
book1.save();
}
public void deleteData(View view) {
// LitePal.delete(Book.class, 1); // 根據(jù)id刪除指定的數(shù)據(jù)
LitePal.deleteAll(Book.class, "name = ?", "Android進階"); // 根據(jù)條件刪除數(shù)據(jù)
}
public void queryData(View view) {
List<Book> bookList = LitePal.findAll(Book.class); // 查詢所有數(shù)據(jù)
for (Book book : bookList) {
Log.d(TAG, "書名: " + book.getName());
Log.d(TAG, "作者: " + book.getAuthor());
Log.d(TAG, "頁數(shù): " + book.getPages());
Log.d(TAG, "價格: " + book.getPrice());
}
}
public void modifiedData(View view) {
Book book = new Book();
book.setName("Android進階");
book.updateAll("name = ?", "大教學(xué)論");
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/create_database"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="創(chuàng)建數(shù)據(jù)庫"
android:onClick="CreateDatabase"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.13" />
<Button
android:id="@+id/add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加數(shù)據(jù)"
android:onClick="addData"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.278" />
<Button
android:id="@+id/del_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除數(shù)據(jù)"
android:onClick="deleteData"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.395" />
<Button
android:id="@+id/query_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查詢數(shù)據(jù)"
android:onClick="queryData"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.512" />
<Button
android:id="@+id/motified_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改數(shù)據(jù)"
android:onClick="modifiedData"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.651" />
</androidx.constraintlayout.widget.ConstraintLayout>
litepal.xml:?
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="BookStore" ></dbname>
<version value="1" ></version>
<list>
<mapping class="com.example.litepaldemo.Book"></mapping>
</list>
</litepal>
運行結(jié)果:?
?四、異常修復(fù)
使用LItePal:報錯element ‘litepal‘ must be declared和Class referenced in the manifest, `org.litepal.LitePa_敬往事一杯酒哈的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-637359.html
到了這里,關(guān)于安卓:LitePal操作數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!