參考文章
- Android數(shù)據(jù)庫高手秘籍(一)——SQLite命令
- Android數(shù)據(jù)庫高手秘籍(二)——?jiǎng)?chuàng)建表和LitePal的基本用法
- Android數(shù)據(jù)庫高手秘籍(三)——使用LitePal升級表
- Android數(shù)據(jù)庫高手秘籍(四)——使用LitePal建立表關(guān)聯(lián)
- Android數(shù)據(jù)庫高手秘籍(五)——LitePal的存儲(chǔ)操作
- Android數(shù)據(jù)庫高手秘籍(六)——LitePal的修改和刪除操作
- Android數(shù)據(jù)庫高手秘籍(七)——體驗(yàn)LitePal的查詢藝術(shù)
- Android數(shù)據(jù)庫高手秘籍(八)——使用LitePal的聚合函數(shù)
- LitePal 1.4.0版本發(fā)布,你們要的多數(shù)據(jù)庫功能終于來了
- LitePal 1.5.0版本發(fā)布,你想要的都在這里
- LitePal 1.6.0版本來襲丙躏,數(shù)據(jù)加解密功能保障你的應(yīng)用數(shù)據(jù)安全
Include library
implementation 'org.litepal.android:core:1.6.1'
Configure litepal.xml
Create a file in the assets folder of your project and name it as litepal.xml. Then copy the following codes into it.
<?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>
Configure LitePalApplication
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
}
ProGuard
-keep class org.litepal.** {
*;
}
-keep class * extends org.litepal.crud.DataSupport {
*;
}
注意事項(xiàng)
id關(guān)鍵字問題
如果實(shí)體中包含id字段眉抬,會(huì)被系統(tǒng)保留關(guān)鍵字id覆蓋并且自增長,處理方法為:重命名為其他字段名
@Column(unique = true)
@SerializedName("id")
private int articleId;
如上第焰,服務(wù)器傳過來的id字段,SerializedName解決Gson映射問題。
保存的時(shí)候诽嘉,保證唯一性,避免多次保存同一記錄
article.saveOrUpdate("articleid = ?", String.valueOf(article.getArticleId()));