Android xUtils3 Selector expr()查询用法

查询的bean类如下所以

@Table(name="student")
private class Student {
    @Column(name = "id", isId = true)
    public int id;
    @Column(name = "name")
    public String name;
    @Column(name = "math_score")
    public int mathScore;
    @Column(name = "history_score")
    public int historyScore;
}

我要实现的查询是查询出数学成绩比历史成绩高的同学,原来的查询语句如下,但是查询出来却不是我要的结果

仔细分析下("math_score", ">", "history_score")的写法其实可以发现问题的所在,因为xUtils3框架第一个参数肯定是认为是个字段名,第二个参数是个比较符,第三个参数就无法确定你到底传入的是一个值,还是一个字段名,所以我认为xUtils参数只支持第三个参数是个值,而不支持两个字段的比较。

try {
    mDbManager.selector(Student.class).where("math_score", ">", "history_score").findAll();
} catch (DbException e) {
    e.printStackTrace();
}

既然上面的写法不支持,只能采用最原始的表达式的方法,但是却抛异常了

mDbManager.selector(Student.class).expr("select * from student where math_score>history_score").findAll();

断点后发现如上写法拼出来的sql语句为"select * from student where select * from student where math_score>history_score"

说明xUtils3已经替我们拼上了前面的"select * from student where",所以我们无需再加这句话

最终的写法如下

mDbManager.selector(Student.class).expr("math_score>history_score").findAll();

 

posted @ 2019-09-04 16:44  野猿新一  阅读(28)  评论(0编辑  收藏  举报