Problems when using LitePal

There was a problem updating to default

Problem Description

Define a Student table.

public class Student extends LitePalSupport {
    
    
    //主键
    public long id;
    //姓名
    public String name;
    //是否是男生,且设置默认值为false
    public boolean isMale = false;
    //学号
    public String sno;

    @Override
    public String toString() {
    
    
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", isMale=" + isMale +
                ", sno='" + sno + '\'' +
                '}';
    }
}

Keep a student record.

        Student student = new Student();
        student.name = "张三";
        student.isMale = true;
        student.sno = "0001";
        student.save();

Execute the following code to update the isMale field

        Student student = LitePal.where("name = ?", "张三")
                .findFirst(Student.class);
        Logger.d("原student: "+student);
        student.isMale = false;//更新isMale字段为false
        boolean isUpdateSucc = student.update(student.id) > 0;
        Logger.d("更新isMale是否成功:"+isUpdateSucc);
        student = LitePal.where("name = ?", "张三")
                .findFirst(Student.class);
        Logger.d("新student: "+student);

The output log is as follows:

原student: Student{
    
    name='张三', isMale=true, sno='0001'}
更新isMale是否成功:true
新student: Student{
    
    name='张三', isMale=true, sno='0001'}

It can be seen from the log that the isMale field is not updated successfully.
The reason is that LitePal's update method will only update the field if the field value is not the default value.

Solution

Method 1: Use setToDefaultmethod

        Student student = LitePal.where("name = ?", "张三")
                .findFirst(Student.class);
        Logger.d("原student: "+student);
//        student.isMale = false;//更新isMale字段为false
        student.setToDefault("isMale");//更新isMale字段为false
        boolean isUpdateSucc = student.update(student.id) > 0;
        Logger.d("更新isMale是否成功:"+isUpdateSucc);
        student = LitePal.where("name = ?", "张三")
                .findFirst(Student.class);
        Logger.d("新student: "+student);

The output log is as follows:

原student: Student{
    
    name='张三', isMale=true, sno='0001'}
更新isMale是否成功:true
新student: Student{
    
    name='张三', isMale=false, sno='0001'}

Method 2: Use save()the method to save data instead

        Student student = LitePal.where("name = ?", "张三")
                .findFirst(Student.class);
        Logger.d("原student: "+student);
        student.isMale = false;//更新isMale字段为false
        boolean isUpdateSucc = student.save();//改成用save()保存字段
        Logger.d("更新isMale是否成功:"+isUpdateSucc);
        student = LitePal.where("name = ?", "张三")
                .findFirst(Student.class);
        Logger.d("新student: "+student);

The output log is as follows:

原student: Student{
    
    name='张三', isMale=true, sno='0001'}
更新isMale是否成功:true
新student: Student{
    
    name='张三', isMale=false, sno='0001'}

save()The method is to overwrite each field directly, no matter whether the current field value is the default value or not.
However, it should be noted that if the table has another table associated with it, this method is not recommended, because the foreign key of the associated table will be cleared.

Reference method:
Update data: cannot be updated to 0 from other types

There was a problem updating the associated table

Problem Description

Student and Book is a one-to-many relationship.

public class Student extends LitePalSupport {
    
    
    //主键
    public long id;
    //姓名
    public String name;
    //是否是男生,且设置默认值为false
    public boolean isMale = false;
    //学号
    public String sno;

    public List<Book> books = new ArrayList<>();

    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", isMale=" + isMale +
                ", sno='" + sno + '\'' +
                ", books=" + books +
                '}';
    }
}

public class Book extends LitePalSupport {
    
    
    //主键
    public long id;
    //书名
    public String name;

    public Student student;

    @Override
    public String toString() {
    
    
        return "Book{" +
                "name='" + name + '\'' +
                '}';
    }
}

Add 2 books for "Zhang San"

        Student student = LitePal.where("name = ?", "张三").findFirst(Student.class);

        Book chineseBook = new Book();
        chineseBook.name = "语文";
        chineseBook.student = student;
        chineseBook.save();

        Book mathBook = new Book();
        mathBook.name = "数学";
        mathBook.student = student;
        mathBook.save();

At this time, the Student table and Book table are as
insert image description here
insert image description here
follows Next, update Zhang San’s student number and add a book for Zhang San

        Student student = LitePal.where("name = ?", "张三").findFirst(Student.class);
        Logger.d("查询到的student: "+student);
        student.sno = "0002";
        student.save();

        Book englishBook = new Book();
        englishBook.name = "英语";
        englishBook.student = student;
        englishBook.save();

After executing the above code, the output log is as follows

查询到的student: Student{
    
    name='张三', isMale=true, sno='0001', books=[]}

At this time, the Student table and Book table are as follows

insert image description here
insert image description here
It can be seen from the figure that the foreign keys of the original language books and mathematics books are left blank. Because the default mode of LitePal is lazy query. Therefore, when Zhang San is queried, Zhang San's booksis an empty set. At this time, if you call save()the method directly, LitePal will think that you want to clear the relationship, so the foreign keys of the Chinese book and the math book are all blank.

Solution

Method 1: Use update()update instead

        Student student = LitePal.where("name = ?", "张三").findFirst(Student.class);
        Logger.d("查询到的student: "+student);
        student.sno = "0002";
        student.update(student.id);

        Book englishBook = new Book();
        englishBook.name = "英语";
        englishBook.student = student;
        englishBook.save();

Execute the above code, the output log is as follows

查询到的student: Student{
    
    name='张三', isMale=true, sno='0001', books=[]}

At this time, the Student table and Book table are as follows
insert image description here
insert image description here

Method Two: Use Aggressive Queries Instead

        Student student = LitePal.where("name = ?", "张三").findFirst(Student.class, true);//此处改用激进查询
        Logger.d("查询到的student: "+student);
        student.sno = "0002";
        student.save();

        Book englishBook = new Book();
        englishBook.name = "英语";
        englishBook.student = student;
        englishBook.save();

After executing the above code, the following log is output

查询到的student: Student{
    
    name='张三', isMale=true, sno='0001', books=[Book{
    
    name='语文'}, Book{
    
    name='数学'}]}

The results of the Student table and the Book table are the same as method one.

Reference method:
use save to update the data in the data table instead of update, and then the table data that uses the id of the table as the foreign key will not be queried

Problems when splicing where query statements

Problem Description

An exception is thrown when the concatenated field value is a Chinese character

    String whereName = "name = 张三";
    Student student = LitePal.where(whereName).findFirst(Student.class);
    Logger.d("查询到的student: "+student);

Executing the above code will throw the following exception
LitePalSupportException: no such column: Zhang San (code 1): , while compiling: SELECT * FROM student WHERE name = Zhang San LIMIT 1

When the concatenated field value is a numeric string whose first digit is 0, no records can be queried

   String whereSno = "sno = 0001";
   Student student = LitePal.where(whereSno).findFirst(Student.class);
   Logger.d("查询到的student: "+student);

Execute the above code, the output log is as follows

查询到的student: null

Solution

Method 1: Enclose the field value in single quotes when splicing

//  String whereName = "name = 张三";
    String whereName = "name = '张三'";
    Student student = LitePal.where(whereName).findFirst(Student.class);
    Logger.d("查询到的student: "+student);

//  String whereSno = "sno = 0001";
    String whereSno = "sno = '0001'";
    Student student = LitePal.where(whereSno).findFirst(Student.class);
    Logger.d("查询到的student: "+student);

After executing the above code, the output log is as follows

查询到的student: Student{
    
    name='张三', isMale=true, sno='0001'}
查询到的student: Student{
    
    name='张三', isMale=true, sno='0001'}

Method 2: Use LitePal's placeholder(?)

    Student student = LitePal.where("name = ?", "张三").findFirst(Student.class);
    Logger.d("查询到的student: "+student);

    student = LitePal.where("sno = ?", "0001").findFirst(Student.class);
    Logger.d("查询到的student: "+student);

After executing the above code, the output log is as follows

查询到的student: Student{
    
    name='张三', isMale=true, sno='0001'}
查询到的student: Student{
    
    name='张三', isMale=true, sno='0001'}

Guess you like

Origin blog.csdn.net/jiejingguo/article/details/124496642