LitePal是一個便于開發(fā)者使用SQLite的Android框架,極大的簡化了SQLite的使用,這是它的github地址
https://github.com/LitePalFramework/LitePal
初期準(zhǔn)備
1.導(dǎo)入
在build.gradle中加入依賴
dependencies {
compile 'org.litepal.android:core:1.6.1'
}
2.配置litepal.xml
在app/src/main下新建文件夾assets,在該文件夾下new->Android resource file新建一個xml文件命名為litepal,復(fù)制以下內(nèi)容到這個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>
- dbname 該數(shù)據(jù)庫的名字
- version 該數(shù)據(jù)庫的版本
- list 配置映射類
- storage 配置數(shù)據(jù)的儲存位置,只有internal和external兩個值
3.配置 LitePalApplication
為便于使用,避免不必要的參數(shù)傳遞,在AndroidManifest.xml中作如下配置
<manifest>
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
</manifest>
然后在Application中初始化
public class MyOwnApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
...
}
使用
1.創(chuàng)建數(shù)據(jù)表
定義一個類,類名為表名,成員變量為表中各個屬性,為便于操作,需繼承自DataSupport,假如有Album和Song兩個表
public class Album extends DataSupport {
@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.
...
}
public class Song extends DataSupport {
@Column(nullable = false)
private String name;
private int duration;
@Column(ignore = true)
private String uselessField;
private Album album;
// generated getters and setters.
...
}
然后在litepal.xml中的<List> </list>中添加
<list>
<mapping class="org.litepal.litepalsample.model.Album" />
<mapping class="org.litepal.litepalsample.model.Song" />
</list>
這樣在下次操作數(shù)據(jù)庫的時候就會生成這個表。
得到數(shù)據(jù)庫:
SQLiteDatabase db = LitePal.getDatabase();
2.更新數(shù)據(jù)表
只需要改變該類的成員變量,然后改變litepal.xml中的verson值及可更新數(shù)據(jù)表
<!--
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>
-->
<version value="2" ></version>
3.添加數(shù)據(jù)
為該數(shù)據(jù)表添加數(shù)據(jù)只需新定義一個對象,然后調(diào)用save()就可以添加
Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save(); //svae()方法
Song song1 = new Song();
song1.setName("song1");
song1.setDuration(320);
song1.setAlbum(album);
song1.save(); //svae()方法
Song song2 = new Song();
song2.setName("song2");
song2.setDuration(356);
song2.setAlbum(album);
song2.save(); //svae()方法
4.更新數(shù)據(jù)
調(diào)用find()找到id為i的該行數(shù)據(jù),然后改變相應(yīng)值,調(diào)用save()即可更新該行數(shù)據(jù)
id為數(shù)據(jù)表創(chuàng)建時自動添加的一個屬性
Album albumToUpdate = DataSupport.find(Album.class, 1);
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.save();
如果想一次修改多條數(shù)據(jù),調(diào)用updateAll();
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.updateAll("name = ?", "album");
5.刪除數(shù)據(jù)
刪除單條,用delete()
DataSupport.delete(Song.class, id);
刪除多條
DataSupport.deleteAll(Song.class, "duration > ?" , "350");
6.查找數(shù)據(jù)
查找單條數(shù)據(jù)(根據(jù)id)
Song song = DataSupport.find(Song.class, id);
查找所有數(shù)據(jù)
List<Song> allSongs = DataSupport.findAll(Song.class);
按條件查找并排序
List<Song> songs = DataSupport.where("name like ? and duration < ?", "song%", "200").order("duration").find(Song.class);
7.異步操作
每次數(shù)據(jù)庫操作默認(rèn)是在main thread操作的,如果有耗時查詢,需要用異步操作findAllAsync(),存儲用saveAsync()
操作結(jié)果在onFinish()回調(diào),
DataSupport.findAllAsync(Song.class).listen(new FindMultiCallback() {
@Override
public <T> void onFinish(List<T> t) {
List<Song> allSongs = (List<Song>) t;
}
});
Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.saveAsync().listen(new SaveCallback() {
@Override
public void onFinish(boolean success) {
}
});