gpt4 book ai didi

android - 使用 ORMLITE 在 Android 中的多个查询条件

转载 作者:IT王子 更新时间:2023-10-29 06:31:38 35 4
gpt4 key购买 nike

我想做一个有多个条件的简单查询

我使用 OrmLite 来映射实体对象。

现在我想在我的表中搜索一个对象。

假设我有一个映射 PERSON 表的 Person 实体,我想做的是用一些参数初始化一个对象并搜索它。

假设一个函数searchPerson(Person oPerson)

如果我像这样传递一个对象 OPerson

Id = null
Name = John
Age = null
Sex = male

是否可以编写查询来实现该目标?像这样的伪代码

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();

我知道我可以用这种方式做多个查询

list=PersonDao.queryBuilder().where().eq("name",OPerson.name)
.and().eq("sex",OPerson.sex").query();

但我还想检查该值是否存在

最佳答案

where (if 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();

这利用了 where.and(int) method它在堆栈上获取多个子句,并将它们与之间的 AND 放在一起。

关于android - 使用 ORMLITE 在 Android 中的多个查询条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23713388/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com