Android Litepal基本使用(草稿版)

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 singeralbum 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
    }
});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值