【Android】LitePal操作数据库

正文索引 [隐藏]

LitePal是Android的一个开源库,开源直接映射到面向对象,不用SQL语句来操作数据库,前提先配置一下,非常简单的配置,如果用习惯后可能都不用Android原生的方式了,这个总共来说就两步,第一步如果在AS里面配置好一次,后面都不用配置了。

一、环境配置

1.去Moudle:app的gradle里面添加库

dependencies {
implementation 'org.litepal.android:java:3.0.0'
}

2.项目新建一个Folder-->Assets Folder文件夹,里面创建一个litepal.xml文件
3.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>

LITEPAL00
4.添加AndroidManifest.xml里面的代码
<manifest>
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
</manifest>

配置基本完成了

二、操作数据库

LitePal创建的表默认都有一个主键自动增长的ID字段,你建表的时候都会添加的
1. 创建表
我们用LitePal其实还是比较好理解的,就建Album和Song两张表吧

public class Album extends LitePalSupport {
@Column(unique = true, defaultValue = "unknown")//注解
private String name; //字段名字:name
private float price;
private byte[] cover;
private List songs = new ArrayList();
// generated getters and setters.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public byte[] getCover() {
return cover;
}
public void setCover(byte[] cover) {
this.cover = cover;
}
public List getSongs() {
return songs;
}
public void setSongs(List songs) {
this.songs = songs;
}
}

Song表
public class Song extends LitePalSupport {
@Column(nullable = false)
private String name;
private int duration;
@Column(ignore = true)//注解,忽视后面一句,不建字段
private String uselessField; //建表的时候回忽略这一段
private Album album;
// generated getters and setters.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getUselessField() {
return uselessField;
}
public void setUselessField(String uselessField) {
this.uselessField = uselessField;
}
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
}

然后去litepal.xml里面去映射这两个
<list>
<mapping class="org.litepal.litepalsample.model.Album" />
<mapping class="org.litepal.litepalsample.model.Song" />
</list>

那个是自己的包,不要直接复制了.
2. 插入数据
Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();
Song song1 = new Song();
song1.setName("song1");
song1.setDuration(320);
song1.setAlbum(album);
song1.save();
Song song2 = new Song();
song2.setName("song2");
song2.setDuration(356);
song2.setAlbum(album);
song2.save();

3.更新数据
更新数据有两种方式
第一种
Album albumToUpdate = LitePal.find(Album.class, 1);
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.save();

第二种
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);

其实我比较喜欢第二种,第一种有点不符合逻辑,正常来说你看数据库你根本不知道它的ID的
3. 删除数据
第一种,根据ID来删除,也有点不符合逻辑
LitePal.delete(Song.class, id);

第二种,根据条件来删除
LitePal.deleteAll(Song.class, "duration > ?" , "350");

4. 查询数据
单条数据
Song song = LitePal.find(Song.class, id);

一个表的所有数据
List<Song> allSongs = LitePal.findAll(Song.class);

复杂查询
List<Song> songs = LitePal.where("name like ? and duration < ?", "song%", "200").order("duration").find(Song.class);

具体参考文档LitePal文档