The usage of Litepal and the solution of dbname is empty

Keywords: Database xml Android encoding

The steps to use Litepal are as follows:
1. Introduce JAR package and change configuration
Use Android Studio to add:
dependencies {
compile 'org.litepal.android:core:1.6.1'
}
1.6.1 is the version number, which can be changed according to the requirements.
Then configure the litepal.xml file. Generally, create an assets directory under the app/src/main directory, and then create a litepal.xml file below the directory as follows:

<?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="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>
            <mapping class="com.test.model.Reader" />
            <mapping class="com.test.model.Magazine" />
        <st>
    -->
    <list>
        <mapping 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:
                <storage value="external" />
            -->

            </litepal>

You may be prompted when creating a database that your dbname is empty or null pointer is abnormal. This is because there is a problem with your Litepal.xml file. At first glance, it's right. As long as you use this paragraph, it's OK. Don't ask me why, I don't know.
After that, create a class, that is, the table in your database, and add the absolute path of this class to the label in the xml file above. So you make your own watch
In the actual database operation, once you have operation on this database, when there is no database, it will create by itself.
Of course, we also need to configure it in the AndroidManifest.xml file.
<application
android:name="org.litepal.LitePalApplication">
</application>

Posted by MSK7 on Mon, 04 May 2020 22:10:01 -0700