gpt4 book ai didi

android - 使用 greenDao 动态查询

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:50 27 4
gpt4 key购买 nike

我需要验证一些条件来创建一个完整的查询:

QueryBuilder qb = getMyObjDao().queryBuilder();

if ( someCondition )

 qb.where(MyObjDao.Properties.Prop1.eq(someValue)); 

else
qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue));

if ( someOtherCondition )

 qb.where(MyObjDao.Properties.Prop3.eq(someValue)); 

else

 qb.whereOr(MyObjDao.Properties.Prop4.eq(someValue));

那么是否可以连接查询构建器条件并动态创建查询构建器?或任何东西:

(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and
|(b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and ....

如何在 greenDao 中实现?

最佳答案

QueryBuilder.and()QueryBuilder.or() 用于组合 WhereCondition。生成的 WhereCondition 必须在 QueryBuilder.where()(它将使用 AND 组合条件)或 QueryBuilder 中使用。 whereOr().


请注意,您所有的查询都没有多大意义。因此,我给您的代码可能无法按您的预期工作,因为我只是在猜测您的预期。例如,您可以采用 qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue))

这转换为 where (Prop2 = someValue OR Prop2 = someValue) 或者可能转换为 where (Prop2 = 'someValue' OR Prop2 = 'someValue')。在它们每个中,OR 和第二个语句都已过时。

另一个例子是 (a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and (b = '%'+condition2 or b = ' %'+condition2+'%' 或 b = condition2 + '%') and ....如果您不搜索其中包含 % 的字符串,则带有这样的 where 子句的查询将不返回任何结果或返回错误结果。


对于:

(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and
(b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and ....

你可以这样使用:

ArrayList<WhereCondition> and = new ArrayList<WhereCondition>();

if (condition1 != null) {
and.add(queryBuilder.or(
YourDao.Properties.a.eq("%"+condition1),
YourDao.Properties.a.eq("%"+condition1+"%"),
YourDao.Properties.a.eq(condition1+"%")));
}

if (condition2 != null) {
and.add(queryBuilder.or(
YourDao.Properties.a.eq("%"+condition2),
YourDao.Properties.a.eq("%"+condition2+"%"),
YourDao.Properties.a.eq(condition2+"%")));
}

...

if (and.size() == 1) {
queryBuilder.where(and.get(0));
} else if (and.size() > 1) {
WhereCondition first = and.remove(0);
queryBuilder.where(first, and.toArray(new WhereCondition[and.size()]));
}

更新

当然你也可以使用另一种没有动态列表的方法:

对于:

QueryBuilder qb = getMyObjDao().queryBuilder();

if ( someCondition )

qb.where(MyObjDao.Properties.Prop1.eq(someValue));

else
qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue));

if ( someOtherCondition )

qb.where(MyObjDao.Properties.Prop3.eq(someValue));

else

qb.whereOr(MyObjDao.Properties.Prop4.eq(someValue));

你可以使用这个:

QueryBuilder<MyObj> queryBuilder = getMyObjDao().queryBuilder();
WhereCondition where = null;

if (someCondition1) {
WhereCondition cond = MyObjDao.Properties.Prop1.eq(someValue);
if (where == null) {
where = cond;
} else {
where = queryBuilder.and(where, cond);
}
} else {
WhereCondition cond = queryBuilder.or(
MyObjDao.Properties.Prop2.eq(someValue),
MyObjDao.Properties.Prop2.eq(someOtherValue));
if (where == null) {
where = cond;
} else {
where = queryBuilder.and(where, cond);
}
}

if (someOtherCondition) {
WhereCondition cond = MyObjDao.Properties.Prop3.eq(someValue);
if (where == null) {
where = cond;
} else {
where = queryBuilder.and(where, cond);
}
} else {
WhereCondition cond = MyObjDao.Properties.Prop4.eq(someValue);
if (where == null) {
where = cond;
} else {
where = queryBuilder.or(where, cond2);
}
}

List<YourObj> result = queryBuilder.where(where).list();

如您所见,您可以使用 greendao 在运行时组合 WhereCondition 的可能性有很多。但是只要你不给出一个详细的例子和描述你真正想做的事情,就没有人能帮助你。

顺便说一句:

  • 如果您只使用一个 WhereCondition,则使用 where()whereOr() 没有任何区别。
  • 你可能想查询:(a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%') 而不是 ( a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%').
  • 注意,(a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%') 等于 (a like '%'+condition1+'%') 因为每个匹配 a like '%'+condition1a like condition1+'%' 的结果也将匹配 一个赞'%'+condition1+'%'.

关于android - 使用 greenDao 动态查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623523/

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