Use of LitePal

1. What is LitePal

LitePal is an open source Android database framework that uses object-relational mapping (ORM) mode to encapsulate common database functions. You can create tables, add, delete, modify, and check without writing a single line of SQL statements.

2. The steps of using LitePal

1. Add dependency:

implementation 'org.litepal.guolindev:core:3.2.3'

2. Create a class Student to inherit LitePalSupport. As you might have guessed, this is equivalent to a table in our database, and the attribute of the bean is the field representing the table. Then create the assets directory under main, create a new litepal.xml file, and write the following in the file:

<?xml version="1.0" encoding="utf-8"?>
    <dbname value="demo"></dbname>
    <version value="1"></version>
        <mapping class="com.example.linkbasic.litepaltest.Student"/>

The meaning in the area means: the value is the name of the database, used to set the version number of the database, used to set all the mapping models, that is, the path of the bean we created, which is actually one of our databases table.

Initialization of LitePal: Initialize LitePal in the onCreate method of MyApplication:


Don’t forget to add android:name=".MyApplication" to the Application of the light file.
Through the above three steps, we will integrate Litepal into our project, and we can add, delete, modify, check and version our database. upgrade.

Three. LitePal addition, deletion, modification and investigation


1. Add a piece of data
directly call method to insert

        Student student = new Student();
        if ( {
            Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "插入失败", Toast.LENGTH_SHORT).show();

2. Insert data in
batches Use LitePal.saveAll (studentList) to insert data in batches to insert a set of data into the table.

        for (int i = 0; i < 10; i++) {
            Student student1 = new Student();
            student1.setName("张三" + i);
            student1.setAge(20 + i);


1. Delete according to id

        LitePal.delete(Student.class, 2);

2. Delete according to conditions

        LitePal.deleteAll(Student.class, "name=?", "张三");

3. Delete the entire table data


4. Directly call the delete method of the object to delete

Student deleteStudent=new Student();


1. Make changes based on id

        ContentValues values1 = new ContentValues();
        values1.put("name", "小一");
        LitePal.update(Student.class, values1, 1);


        Student updateStudent = new Student();

2. Update according to conditions

        ContentValues values2 = new ContentValues();
        values2.put("name", "李四");
        LitePal.updateAll(Student.class, values2, "age>? and name=?", "25", "张三7");


        Student updateStudent2 = new Student();
        updateStudent2.updateAll("age>?", "27");


1. Query a single piece of data

        Student student = LitePal.find(Student.class, 1);
        Student firstNews = LitePal.findFirst(Student.class);//第一条数据
        Student lastNews = LitePal.findLast(Student.class);//最后一条数据

2. Query multiple data and all data

        List<Student> studentList1 = LitePal.findAll(Student.class, 1, 3, 5, 7);
        List<Student> allStudent = LitePal.findAll(Student.class);

3. Query data based on conditions

        List<Student> studentList2 = LitePal.where("age > ?", "23").find(Student.class);
        List<Student> studentList3 ="name").where("age > ?", "23").find(Student.class);//要查询字段
        List<Student> studentList4 ="name")
                .where("age > ?", "24")
                .order("age desc").find(Student.class);//年龄倒叙,asc表示正序排序

The above is the method of adding, deleting, modifying and checking frequently used in our database.

Four. LitePal database upgrade

Our database may need to add new tables or new field attributes in the subsequent development, so we need to upgrade the database, and the upgrade of the LitePal database is also very convenient.
1. To add a new table is to add a class that inherits DataSupport and add it to the litepal.xml file in the assets directory.
2. To add a field, just add a new attribute directly on the basis of the original class
3. Be sure to remember to upgrade the litepal.xml file in the assets directory by +1
4. After the configuration is complete, we also need to manipulate the database to generate new tables or new fields:


It can be placed before operating the database.
Through the above four steps, our database can be upgraded. Is not it simple.