LitePal

前言

郭霖开源项目使用笔记,地址

说明

LitePal映射规则非常轻量级,不像一些其它数据库框架需每模型类单配一映射关系XML,LitePal所有映射自动完成。据LitePal数据类型支持,可进行对象关系映射的数据类型共8种,int、short、long、float、double、boolean、String和Date。只要声明成这8种数据类型的字段都会被自动映射到数据库表中,不需任何额外配置。

实体类中Object属性不会被映射到数据库表中。

配置

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即可。

插入

多次插入数据,数据库表中数据不重复插入。

字段

建表时实体类大写属性自转小写

方法

创建

SQLiteDatabase db = Connector.getDatabase();
或
Connector.getDatabase();


删除

LitePal.deleteDatabase("数据库名");

注意

数据库头次创后改名即创(即同存多数据库),<version value="1" />设版本不可控。

猜你喜欢

转载自blog.csdn.net/zsp_android_com/article/details/80804004