Usage of LitePal Open Source Library

Preface

LitePal is an open source Android library that makes it easy for developers to use the SQLite database. You can do most database operations without even writing SQL statements, including creating or upgrading tables, compressing operations, aggregation functions, etc. LitePal is also easy to set up, and you can integrate it into your project in less than five minutes.

content

Rapid Configuration of LitePal

1. Introducing Jar packages or source code

Use Android Studio to add the following dependencies to the build.gradle of the project:

dependencies {
    compile 'org.litepal.android:core:1.3.2'
}

Using Eclipse to Here After downloading the jar package, copy it to the libs directory of the project even if the introduction is successful.

2. Configure litepal.xml

Create a new litepal.xml file under the assets directory of the project, which reads as follows:

<?xml version="1.0" encoding="utf-8"?>
<litepal>

    <dbname value="cool_weather" />

    <version value="1" />

    <list>
        <mapping class="com.example.yuxuehai.coolweather.bean.Province" />
        <mapping class="com.example.yuxuehai.coolweather.bean.City" />
        <mapping class="com.example.yuxuehai.coolweather.bean.County" />
    </list>

</litepal>

Among them:

  • Is the name of the database
  • Is the version number of the database
  • Is the mapping model of the database (database tables)
  • Is the address of the database mapping model (database table structure)

3. Configure LitePal Application

Configure LitePal Application in Android Manifest.xml as follows:

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

If you already have your own application, you can inherit LitePal Application or add the following code to your code:

public class MyApplication extends LitePalApplication {  
    ...  
}

public class MyApplication extends AnotherApplication{  

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

LitePal's Tables

According to the concept of object-relational mapping pattern, each table should correspond to a model. First, a new model class and a new Province class should be created. The following are the following:

public class Province extends DataSupport {

    private int id;
    private String provinceName;
    private int provinceCode;

    public int getProvinceCode() {
        return provinceCode;
    }

    public void setProvinceCode(int provinceCode) {
        this.provinceCode = provinceCode;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }
}

LitePal's mapping rules are very lightweight, unlike some other database frameworks, which need to configure an XML mapping relationship for each model class separately. All mappings of LitePal are automatically completed. According to LitePal's data type support, there are eight types of data that can be mapped to object relations, int, short, long, float, double, boolean, String and Date. As long as the fields declared as these eight data types are automatically mapped to the database tables, no additional configuration is required.

Note: Only private ly modified fields are mapped to database tables, that is, if a field does not want to be mapped, it can be set to public, protected or default modifiers.

After establishing the Model, we configure it into the mapping list, that is, edit the litepal.xml file under the assest directory, and add the DEST class declaration to the label. Here, we should pay attention to the full class name of the class.

<?xml version="1.0" encoding="utf-8"?>
<litepal>

    <dbname value="cool_weather" />

    <version value="1" />

    <list>
        <mapping class="com.example.yuxuehai.coolweather.bean.Province" />
    </list>

</litepal>

At this point, the LitePal database configuration is completed.

LitePal Upgrade Table

1. Add a new table

First create a new model class, then set it to litepal.xml, as follows:

<?xml version="1.0" encoding="utf-8"?>
<litepal>

    <dbname value="cool_weather" />

    <version value="1" />

    <list>
        <mapping class="com.example.yuxuehai.coolweather.bean.Province" />
        <mapping class="com.example.yuxuehai.coolweather.bean.City" />
        <mapping class="com.example.yuxuehai.coolweather.bean.County" />
    </list>

</litepal>

Then add the version value in litepal.xml as follows:

<?xml version="1.0" encoding="utf-8"?>
<litepal>

    <dbname value="cool_weather" />

    <version value="2" />

    <list>
        <mapping class="com.example.yuxuehai.coolweather.bean.Province" />
        <mapping class="com.example.yuxuehai.coolweather.bean.City" />
        <mapping class="com.example.yuxuehai.coolweather.bean.County" />
    </list>

</litepal>

2. Adding new columns to old tables

First, add a new private ly modified field to the model class that needs to be upgraded, and then add the value of version in litepal.xml, so that the code is not shown here, and the reader can experience it for himself.

Operation of LitePal

To store data, LitePal first inherits DataSupport from the model class.

public class Province extends DataSupport {
    ...
    ...
}

After inheriting the DataSupport classes, these entity classes have the ability to perform CRUD operations.

1. Storage operations

Create an object, assign its attributes, and finally call save() to store it, as follows:

Province province = new Province();
province.setProvinceName(provinceObject.getString("name"));
province.setProvinceCode(provinceObject.getInt("id"));
province.save();

Better yet, the save() method has a return value, and the reader can judge it according to his own needs and then make some operations:

if (mDest.save()) {  
    Toast.makeText(context, "Storage success", Toast.LENGTH_SHORT).show();  
} else {  
    Toast.makeText(context, "Storage failure", Toast.LENGTH_SHORT).show();  
}

2. Modification

If you want to change the provinceCode with id 1 in the Province table to "1", you can write as follows:

ContentValues values = new ContentValues();  
values.put("provinceCode", "1");  
DataSupport.update(Pronvice.class, values, 1);  

//Or use the following method

Province updateNews = new Province();  
updateNews.setProvinceCode(1) 
updateNews.update(1);

If you want to change all IDS in the Province table from "1" to "2", you can write as follows:

ContentValues values = new ContentValues();  
values.put("provinceCode", "2");  
DataSupport.updateAll(DEST.class, values, "provinceCode = ?", "1");  

//Or use the following method

Province updateNews = new Province();  
updateNews.setProvinceCode("2");  
updateNews.updateAll("setProvinceCode = ?", "1");

3.LitePal deletion operation

For example, if we want to delete the record with id 2 in the Province table, we can write as follows:

DataSupport.delete(Province.class, 2);

If you want to delete all data with id of "1" in the Province table, you can write as follows:

DataSupport.deleteAll(Province.class, "destId = ? ", "1");

If you want to delete all the data in the Province table, you can write as follows:

DataSupport.deleteAll(Province.class);

LitePal Query Operations

Query the record with id 1 in the Province table and use LitePal to write as follows:

Province mProvince = DataSupport.find(Province.class, 1);

To get the first data in the Province table, you just need to write as follows:

Province mProvince = DataSupport.findFirst(Province.class);

To get the last piece of data in the Province table, simply write as follows:

Province mProvince = DataSupport.findLast(Province.class);

Query the record with id of 1,2,3 in the Province table and use LitePal to write as follows:

List<Province> mProvince = DataSupport.findAll(Province.class,1,2,3);

To query all data, just write as follows:

List<Province> mProvince = DataSupport.findAll(Province.class);

The author just introduces some simple operations. If you want to learn more operations, please visit guolin Guolin's blog. Here

Keywords: Database xml Android encoding

Added by lemin on Tue, 11 Jun 2019 21:50:39 +0300