Android 开源数据库框架LitePal

1.在build.gradle文件中添加依赖

Edit your build.gradle file and add below dependency:

dependencies {
implementation 'org.litepal.android:core:1.6.1'
}

2.在main下新建assets文件夹,在文件夹下新建litepal.xml文件,内容如下:

Create a file in the assets folder of your project and name it as litepal.xml. Then copy the following codes into it.

<?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="demo" />

    <!--
        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="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 class="com.test.model.Magazine" />
        </list>
    -->
    <list>
    </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>

This is the only configuration file, and the properties are simple.

  • dbname configure the database name of project.
  • version configure the version of database. Each time you want to upgrade database, plus the value here.
  • list configure the mapping classes.
  • storage configure where the database file should be stored. internal and external are the only valid options.

3.配置全局上下文

You don't want to pass the Context param all the time. To makes the APIs simple, just configure the LitePalApplication inAndroidManifest.xml as below:

<manifest>
    <application
        android:name="org.litepal.LitePalApplication"
        ...
    >
        ...
    </application>
</manifest>

如果有自己的application文件,则

public class MyOwnApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        LitePal.initialize(this);
    }
    ...
}

4.新建一个与表头相关联的类

/**
 * Created by jiaweiqiang on 2018/1/17 0017.
 * <p>一个存储一条数据的对象,用于LitePal中映射</p>
 */

public class OnelineData extends DataSupport{

    private int orderNum;
    private String time;
    private String kind;
    private double result;
    private String unit;

    public int getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(int orderNum) {
        this.orderNum = orderNum;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public double getResult() {
        return result;
    }

    public void setResult(double result) {
        this.result = result;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }


}

然后在litepal.xml文件中注册这个类

<list>
        <mapping class="com.jiaweiqiang.tools.OnelineData" />
    </list>

然后进行任意依次数据库操作,就创建了以类为名称的表;

5.保存数据,new一个对象,用getter和setter进行赋值,然后调用save()方法保存即可。

OnelineData data1 = new OnelineData();
            data1.setKind("析出率");
            data1.setOrderNum(i);
            data1.setResult(58.987);
            data1.setTime("2018-01-25 12:34:56");
            data1.setUnit("Bq/m2/s");
            data1.save();

更多内容方位https://github.com/LitePalFramework/LitePal

posted @ 2018-01-23 15:39  JMatrix  阅读(910)  评论(0编辑  收藏  举报