Litepal的用法以及dbname is empty的解决

要使用Litepal的步骤如下:
1.引入JAR包以及更改配置
使用Android Studio在项目的build.gradle中添加:
dependencies {
compile 'org.litepal.android:core:1.6.1'
}

其中1.6.1是版本号,可以根据需求改变。
然后配置litepal.xml文件,一般先在app/src/main目录下新建一个assets 目录,然后在其下面创建一个Litepal.xml文件内容如下:

version="1.0" encoding="utf-8"?>

    --
        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:
        "demo" />
    -->
    "CourseManager" />

    --
        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="6" />

    --
        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>
            class="com.test.model.Reader" />
            class="com.test.model.Magazine" />
        
    -->
    <list>
        class = "com.example.qr_code.QR"/>
        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:
                "external" />
            -->

            

有的可能会在创建数据库的时候提示你的dbname is empty 或者空指针异常,这是因为你的Litepal.xml文件有问题,乍一看没错,只要使用这一段就没问题了,别问我为什么,我也不知道。
之后就是创建一个class,也就是你数据库里面的表,在上面的xml文件中的标签中添加这个类的绝对路径。这样你就生成了自己的表.
在实际的数据库操作中一旦你对这个数据库有操作,在没有数据库的时候,他会自行创建。
当然我们还需要在AndroidManifest.xml文件中配置一下。
android:name="org.litepal.LitePalApplication">

你可能感兴趣的:(Android)