1. 程式人生 > >Android Litepal基本使用(草稿版)

Android Litepal基本使用(草稿版)

Litepal一款用於使用Android資料庫的開源工具

 

https://github.com/LitePalFramework/LitePal

 

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

 

Album
albumToUpdate=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
    }
});