sec*_*ond 4 sqlite android query-builder ormlite
我想做一个简单的查询,有多个条件
我使用OrmLite来映射实体对象.
现在我想在我的表中搜索一个对象.
假设我有一个映射PERSON表的Person实体,我想要做的是用一些参数初始化一个对象并搜索它.
假设一个功能 searchPerson(Person oPerson)
如果我像这样传递一个对象OPerson
Id = null
Name = John
Age = null
Sex = male
Run Code Online (Sandbox Code Playgroud)
是否可以编写查询以达到该目标?像这样的伪代码
pers = (from p in db.Table<Person>()
where (if OPerson.Id !=null) p.Id==OPerson.Id}
AND {(if OPerson.Name !=null) p.Name.Contains(OPerson.Name)}
AND {(if condition) where-contion}
select p).ToList();
Run Code Online (Sandbox Code Playgroud)
我知道我可以用这种方式做多个查询
list=PersonDao.queryBuilder().where().eq("name",OPerson.name).and().eq("sex",OPerson.sex").query();
Run Code Online (Sandbox Code Playgroud)
但我还要检查值是否存在
where(如果OPerson.Id!= null)p.Id == OPerson.Id}
@ArghArgh很接近,但没有正确的AND.问题是AND语句取决于是否有任何先前的语句.我会这样做:
QueryBuilder<Person, Integer> queryBuilder = dao.queryBuilder();
Where<Person, Integer> where = queryBuilder.where();
int condCount = 0;
if (oPerson.id != null) {
where.eq("id", oPerson.id);
condCount++;
}
if (oPerson.name != null) {
where.like("name", "%" + oPerson.name + "%");
condCount++;
}
...
// if we've added any conditions then and them all together
if (condCount > 0) {
where.and(condCount);
}
// do the query
List<Persion> personList = queryBuilder.query();
Run Code Online (Sandbox Code Playgroud)
这使得使用的where.and(int)方法,这需要数堆栈上的条款,并与之间与运算放在一起.
| 归档时间: |
|
| 查看次数: |
3654 次 |
| 最近记录: |