Litepal一款用于使用Android数据库的开源工具
https://github.com/LitePalFramework/LitePal
简要使用方法说明
1.添加依赖
dependencies {
implementation 'org.litepal.android:java:3.0.0'
}
2.在assets目录下配置litepal.xml文件
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="BookStore"></dbname>
<version value="1"></version>
<list>
<mapping class="包名.实体类名"/>
</list>
</litepal>
3.AndroidManifest中设置application
android:name="org.litepal.LitePalApplication"
4.创建数据表的实体类,在list标签中添加
5.Litepal.getDatabase()
6.之后的增删改查都用实体类对象操作
7.如果更新数据库中表的内容,改变version的版本号和list标签即可
_________________________分割线__________________________
以下是官方教程,直接复制过来看看,也可以去GitHub上自己去看
SQLiteDatabasedb=LitePal.getDatabase();
Upgrade tables
<versionvalue="2" />
Save data
Albumalbum=newAlbum();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();
Update data
AlbumalbumToUpdate=LitePal.find(Album.class, 1);
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.save();
AlbumalbumToUpdate=newAlbum();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);
AlbumalbumToUpdate=newAlbum();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.updateAll("name = ?", "album");
Delete data
LitePal.delete(Song.class, id);
LitePal.deleteAll(Song.class, "duration > ?" , "350");
Query data
Songsong=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);
Async operations
Every database operation is on main thread by default. If your operation might spent a long time, for example saving or querying tons of records. You may want to use async operations.
LitePal support async operations on all crud methods. If you want to find all records from song table on a background thread, use codes like this.
LitePal.findAllAsync(Song.class).listen(newFindMultiCallback<Song>() {
@Override
publicvoidonFinish(List<Song>allSongs) {
}
});
Just use findAllAsync() instead of findAll(), and append a listen() method, the finding result will be callback to onFinish()method once it finished.
Abd saving asynchronously is quite the same.
Java:
Albumalbum=newAlbum();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.saveAsync().listen(newSaveCallback() {
@Override
publicvoidonFinish(booleansuccess) {
}
});
Multiple databases
If your app needs multiple databases, LitePal support it completely. You can create as many databases as you want at runtime. For example:
LitePalDBlitePalDB=newLitePalDB("demo2", 1);
litePalDB.addClassName(Singer.class.getName());
litePalDB.addClassName(Album.class.getName());
litePalDB.addClassName(Song.class.getName());
LitePal.use(litePalDB);
This will create a demo2 database with singer, album and song tables.
If you just want to create a new database but with same configuration as litepal.xml, you can do it with:
LitePalDBlitePalDB=LitePalDB.fromDefault("newdb");
LitePal.use(litePalDB);
You can always switch back to default database with:
LitePal.useDefault();
And you can delete any database by specified database name:
LitePal.deleteDatabase("newdb");
Listen database create or upgrade
If you need to listen database create or upgrade events and fill some initial data in the callbacks, you can do it like this:
LitePal.registerDatabaseListener(newDatabaseListener() {
@Override
publicvoidonCreate() {
// fill some initial data
}
@Override
publicvoidonUpgrade(intoldVersion, intnewVersion) {
// upgrade data in db
}
});