配置相關檔案
在使用之前要進行相關配置,首先要在app.build.gradle中引入LitePal的依賴(下面最後一行代碼)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'org.litepal.guolindev:core:3.2.3'
}
然後在main下建立檔案夾assets,并在其下建立litepal.xml

并在litepal.xml中寫入以下資訊,資料庫名字為BookStore,list标簽中放的是資料庫中的資料表,這裡有兩張表,Book和Category
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!-- 資料庫名字 -->
<dbname value="BookStore" />
<!-- 資料庫版本 -->
<version value="2" />
<!-- 資料庫對象 -->
<list>
<mapping class="com.example.litepaltest.Book"></mapping>
<mapping class="com.example.litepaltest.Category"/>
</list>
</litepal>
再AndroidMainfest.xml中注冊LitePal
以上就是LitePal的配置過程,配置好之後就可以正式開始建立資料庫并對它進行CURD(增删查改)了
建立資料庫
建立一個Book類,資料庫中的一張Book表,讓它繼承于LitePalSupport(書中是DataSupport,已經被棄用,現在改用LitePalSupport),get和set方法建立快捷鍵(Alt+Insert)
public class Book extends LitePalSupport {
private int id;
private String author;
private double price;
private int pages;
private String name;
public int getId() {
return id;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public int getPages() {
return pages;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(double price) {
this.price = price;
}
public void setPages(int pages) {
this.pages = pages;
}
public void setName(String name) {
this.name = name;
}
}
回到litepal.xml中,引入此資料表,上面代碼已經給出,看下面截圖。
在activity_main.xml中建立一個按鈕,用于建立資料庫。
<Button
android:id="@+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="建立資料庫"/>
在MainActivity中設定此按鈕的監聽器,點選按鈕的時候建立資料庫和裡面的資料表。,這裡給出相關代碼。調用Connector.getDatabase()即可建立,是不是很簡單。
Button CreateDatabase=findViewById(R.id.btn_create);
CreateDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Connector.getDatabase();
Toast.makeText(MainActivity.this, "建立成功", Toast.LENGTH_SHORT).show();
}
});
建立成功之後可以使用adb shell進入裝置,然後使用相關指令去檢視資料庫和資料表是否存在,存在就說明建立成功。但是到目前為止我們隻有空表,也就是說表裡面沒有資料的。
添加資料
想要看到資料,就要往裡面添加資料,這就涉及到CURD中的添加資料了,下面來看看怎麼使用LitePal往資料庫中添加資料。
同樣我們先設定一個按鈕,用來添加資料。
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加資料"/>
如果我們要在Book中添加一項press,就直接在裡面添加就行,很簡單
private String press;
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
如果要添加一張表呢,那就再建立一個類,比如Category類,在裡面定義相關的表項。切記要去litepal.xml中引入這張表。當然資料庫的版本也要更新了,原來是1,現在更新為2了。
看看到現在我們建立了哪些内容。
litepal存放資料庫的資訊,Book和Category存放兩張資料表的内容
然後該監聽按鈕了,建立一個Book對象,調用set方法設定我們要添加的資料,最後調用save()方法儲存即可,再到資料庫中檢視,發現可以看到Book表中有了一條資料。
Button AddData=findViewById(R.id.btn_add);
AddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Book book=new Book();
book.setName("The Da Vinci Code");
book.setAuthor("Dan Brown");
book.setPages(454);
book.setPrice(16.96);
book.setPress("Unknown");
book.save();
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}
});
更新資料
更新資料的方法有好幾種,這裡就隻介紹最友善靈巧的一種。
同樣要設定一個按鈕。
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新資料"/>
然後監聽按鈕事件
Button UpdataData=findViewById(R.id.btn_update);
UpdataData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Book book=new Book();
//設定要更新的内容以及更新以後的資料
book.setPrice(14.95);
book.setPress("Anchor");
//設定更新條件,name是The Lost Symbol且author是Dan Brown的全部更新為以上設定
book.updateAll("name=? and author=?","The Lost Symbol","Dan Brown");
Toast.makeText(MainActivity.this,"更新成功",Toast.LENGTH_SHORT).show();
}
});
最後的updateAll裡面的參數,是不是有點像SQL語句呢。點選按鈕以後,會發現所有name是The Lost Symbol且author是Dan Brown的行的Price都變成了14.95、Press都變成了Anchor。
注意:再使用updateAll的時候,會涉及到一個預設值的情況,int的預設值為0,String的預設值是null,如果我們要設定某一項的内容為預設值,就不能再使用set()方法了,而應該使用setToDefault()方法。
錯誤的寫法:
book.setId(0);
正确的寫法:
book.setToDefalut("id")
update()方法沒有指定資料的時候就預設對所有資料都進行修改。
删除資料
删除使用的是LitePal.deleteAll()方法(書中使用的是DataSupport.deleteAll()方法,此方法已被棄用),我們直接看代碼。
設定按鈕
<Button
android:id="@+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除資料"/>
監聽按鈕
Button DeleteData=findViewById(R.id.btn_delete);
DeleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LitePal.deleteAll(Book.class,"price<?","15");//删除所有價格小于15的資料
Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
}
});
第一個參數用于指定我要操作的表Book,後面的參數是資料設定的條件,如果不設定則預設删除所有。
查詢資料
使用LitePal.findAll()方法(書中使用的是DataSupport.findAll()方法,此方法已被棄用)
設定按鈕
<Button
android:id="@+id/btn_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查詢資料"/>
監聽按鈕,這是查詢Book表中的所有資料
Button QueryData=findViewById(R.id.btn_query);
QueryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Book> books = LitePal.findAll(Book.class);//查詢的資料存放到List中
for (Book book : books){//周遊List取出資料并列印
Log.d("MainActivity", book.getName());
Log.d("MainActivity", book.getAuthor());
Log.d("MainActivity", Integer.toString(book.getPages()));
Log.d("MainActivity", Double.toString(book.getPrice()));
}
Toast.makeText(MainActivity.this,"查詢成功",Toast.LENGTH_SHORT).show();
}
});
查詢第一條資料
List<Book> books =LitePal.findFirst(Book.class);
查詢最後一條資料
List<Book> books =LitePal.findLast(Book.class);
還可以使用select進行查詢
List<Book> books =LitePal.select("name","author","pages")
.where("pages>?","400")
.order("pages")
.limit(10)
.offset(10)
.find(Book.class);
limit(10)表示前10行,offset(10)表示偏移為10,也就是11-20行,order("pages")表示結果按照pages升序排列。
這段代碼表示查詢Book表中第11-20條滿足頁數大于400這個條件的name,author,pages這3列資料,并将查詢結果按照頁數升序排列。
當然,如果以上的API還不能滿足查詢需求,LitePal還支援原生的SQL查詢,調用LitePal.findBySQL()方法。注意此方法傳回的是一個Cursor對象,要用前面的方法取出資料。
Cursor cursor=LitePal.findBySQL("select * from Book where pages > ? and price < ?","400","20");
最後附上查詢結果圖
指令行檢視
點選查詢按鈕得到的結果
以上就是使用LitePal操作資料庫的全部過程。