首先说下LitePal是一款非常好用的开源Android库,用于简化SQLite数据库的操作,使用也起来非常的方便。
1.先在你项目下的 build.gradle里添加依赖项
使用java开发android的使用下面的依赖项:
dependencies {
implementation 'org.litepal.android:java:3.0.0'
}
而如果你使用Kotlin就使用下面这个依赖项:
dependencies {
implementation 'org.litepal.android:kotlin:3.0.0'
}
2.要配置你的litepal.xml,可以在你main目录下新建一个资源文件夹assets,然后在配置如下:
<?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:
定义应用程序的数据库名称。默认情况下,每个数据库名称都应该以.db结尾。
如果没有将数据库命名为.db,LitePal将自动为您加上后缀。例如:
<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:
使用映射标记在列表中定义模型,LitePal将为每个映射类创建表。
模型中定义的受支持字段将映射到列中。例如:
<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:
定义.db文件的位置。“内部”意味着.db文件将存储在无法访问的内部存储的数据库文件夹中。
“外部”意味着.db文件将存储在主外部存储设备上的目录路径中,
应用程序可以在该路径中放置其拥有的持久文件,每个人都可以访问这些文件。
“内部”将作为默认。例如:
<storage value="external" />
-->
</litepal>
3.配置项目的Application.xml
<manifest>
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
</manifest>
但如果你的项目已经配置了android:name,那么可以在onCreate()方法中加LitePal.initialize(this);
如下:
public class MyOwnApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
...
}
4.配置步骤就完成了,开始建表。只需在你的javabean中继承LitePalSupport ,如下:
public class Album extends LitePalSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
private float price;
private byte[] cover;
private List<Song> songs = new ArrayList<Song>();
// generated getters and setters.
...
}
5.增加数据:
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();
6.删除数据:
LitePal.delete(Song.class, id);
带条件删除多行数据;
LitePal.deleteAll(Song.class, "duration > ?" , "350");
7.修改数据:
直接赋值后保存
Album albumToUpdate = LitePal.find(Album.class, 1);
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.save();
或者使用update(id),更新某一条数据,如:
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);
还可以使用.updateAll("name = ?", "album")带条件,更新多条数据,如:
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.updateAll("name = ?", "album");
8.查询数据:
查询某一条数据
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").find(Song.class);
或
List<Song> songs = LitePal.where("name like ? and duration < ?", "song%", "200").order("duration").find(Song.class);
参考自https://github.com/LitePalFramework/LitePal,如需访问LitePal的GitHub了解更多也是此链接;