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"?>
<litepal>
    <dbname value="demo"></dbname>
    <!--通过这个加一以及实体类属性修改进行版本升级-->
    <version value="1"></version>
    <list>
        <mapping class="com.example.linkbasic.litepaltest.Student"/>
    </list>
</litepal>

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.

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

LitePal.initialize(this);

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

increase

1. Add a piece of data
directly call student.save() method to insert

//单个插入
        Student student = new Student();
        student.setAge(20);
        student.setName("张三");
        if (student.save()) {
            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);
            studentList.add(student1);
        }
        LitePal.saveAll(studentList);

delete

1. Delete according to id

//方式一(根据id删除)
        LitePal.delete(Student.class, 2);

2. Delete according to conditions

//方式二(根据条件删除)
        LitePal.deleteAll(Student.class, "name=?", "张三");

3. Delete the entire table data

//方式三(删除表所有数据)
        LitePal.deleteAll(Student.class);

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

Student deleteStudent=new Student();
        if(deleteStudent.isSaved()){//只有保存到数据库的数据调用delete方法才有效
            deleteStudent.delete();
        }

change

1. Make changes based on id

//方式一(根据id更新)
        ContentValues values1 = new ContentValues();
        values1.put("name", "小一");
        LitePal.update(Student.class, values1, 1);

or

//方式三(根据id更新)
        Student updateStudent = new Student();
        updateStudent.setName("王五");
        updateStudent.update(3);

2. Update according to conditions

//方式二(根据条件更新)
        ContentValues values2 = new ContentValues();
        values2.put("name", "李四");
        LitePal.updateAll(Student.class, values2, "age>? and name=?", "25", "张三7");

or

//方式四(根据条件更新)
        Student updateStudent2 = new Student();
        updateStudent2.setName("巴拉巴拉");
        updateStudent2.updateAll("age>?", "27");

check

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 = LitePal.select("name").where("age > ?", "23").find(Student.class);//要查询字段
        List<Student> studentList4 = LitePal.select("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:

Connector.getDatabase();

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