前言
郭霖开源项目使用笔记。
说明
- LitePal映射规则非常轻量级,不像一些其它数据库框架需每模型类单配一映射关系XML,LitePal所有映射自动完成。据LitePal数据类型支持,可进行对象关系映射的数据类型共8种,int、short、long、float、double、boolean、String和Date。只要声明成这8种数据类型的字段都会被自动映射到数据库表中,不需任何额外配置。
- 实体类中Object属性不会被映射到数据库表中。
方法
创建
SQLiteDatabase db = Connector.getDatabase();
或
Connector.getDatabase();
删除
LitePal.deleteDatabase("数据库名");
列设默值
DrillingFluidMaterial drillingFluidMaterial = new DrillingFluidMaterial();
drillingFluidMaterial.setToDefault("visibleState");
drillingFluidMaterial.updateAll();
setToDefault()传要改字段名(类中字段名)
排序
asc正序
LitePal.order("widtheditdateAssist aes").find(Inputone.class);
desc倒序
LitePal.order("widtheditdateAssist desc").find(Inputone.class);
一次仅单字段排序(多字段无效)
用前注意本地数据库已排序否
配置
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>
-->
<dbname value="basicConfiguration " />
<!--
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="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>
<mapping class="com.test.model.Magazine"></mapping>
</list>
-->
<list>
<mapping class="bean.sync.Block" />
<mapping class="bean.sync.Ground" />
<mapping class="bean.sync.ComplexType" />
<mapping class="bean.sync.IntegratedMachineSieveCloth" />
<mapping class="bean.sync.DrillingFluidMaterial" />
</list>
<!--
Define the cases of the tables and columns name.
Java is a case sensitive language, while database is case insensitive.
LitePal will turn all classes names and fields names into lowercase by default while creating or upgrading database.
Developers can change this behavior into the styles their like.
"keep" will keep the cases of classes and fields.
"upper" will turn all classes names and fields names into uppercase.
"lower" will act as default.
Do not change the value after you run your app for the first time, or it might cause the exception that column can not be found.
value options: keep lower upper
For example:
<cases value="lower" ></cases>
-->
<!--
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="internal"></storage>
-->
</litepal>
数据库名不可大写字母开头
<storage value="internal" />数据库建于data->data->包名->databases
<storage value="guolin/database" />数据库建于SD卡guolin/database目录
litepal.xml
放于assets
下一级包
二或其它级包报错
org.litepal.exceptions.InvalidAttributesException: dbname is empty or not defined in litepal.xml file, or your litepal.xml file is missing.
初始化
一
public class MyOwnApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
...
}
二
public class App extends LitePalApplication {
@Override
public void onCreate() {
super.onCreate();
...
}
...
}
主键
实体类无字段id
时LitePal默置主键id
(int或long),实体类有字段id
时直被LitePal当作主键(int或long)。服务器返实体类字段id
为String类型时因直被LitePal当作主键致主键应有数据类型(int或long)与返实体类字段id
数据类型(String)冲突,此时服务器返实体类字段id
更名为serviceId
即可。
插入
多次插入数据,数据库表中数据不重复插入。
字段
建表时实体类大写属性自转小写
注意
- 数据库头次创后改名即创(即同存多数据库),
<version value="1" />
设版本不可控。 - 默0,同法改1成功改回0失败。
改1 drillingFluidMaterial.setVisibleState(1); drillingFluidMaterial.updateAll("serviceid = ? and hgmc = ?", drillingFluidMaterial.getServiceId(), drillingFluidMaterial.getHgmc()); 改回0 drillingFluidMaterial.setVisibleState(0); drillingFluidMaterial.updateAll("serviceid = ? and hgmc = ?", drillingFluidMaterial.getServiceId(), drillingFluidMaterial.getHgmc());
- 修改数据不改主键值,保存数据主键值增1。
- 一对多 ,一增则多增并自设对应外键;一改且连带多数据变化则多变化部分无外键。
- 所建表含
baseObjId
字段
- 注意命名,否实体类字段用SQL关键字(如
username
)致创表报错。an exception that indicates there was an error with sql parsing or execution