LitePal 一

前言

郭霖开源项目使用笔记。

说明

  • 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
    

参考

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snpmyn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值