Rol*_*low 4 sqlite android greendao
我需要验证一些条件来创建一个完整的查询:
QueryBuilder qb = getMyObjDao().queryBuilder();
if(someCondition)
Run Code Online (Sandbox Code Playgroud)qb.where(MyObjDao.Properties.Prop1.eq(someValue));else
qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue));if(someOtherCondition)
Run Code Online (Sandbox Code Playgroud)qb.where(MyObjDao.Properties.Prop3.eq(someValue));其他
Run Code Online (Sandbox Code Playgroud)qb.whereOr(MyObjDao.Properties.Prop4.eq(someValue));
那么是否可以连接查询构建器条件并动态创建查询构建器?或其他任何事情:
(a ='%'+ condition1或a ='%'+ condition1 +'%'或a = condition1 +'%')和
|(b ='%'+ condition2或b ='%'+ condition2 +'%'或b = condition2 +'%')和....
如何在绿道?
QueryBuilder.and()并QueryBuilder.or()用于组合WhereConditions.结果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+'%' or b = condition2 + '%') and ....
如果你没有在其中搜索字符串%,那么带有这样的where子句的查询将返回none或false结果.
对于:
(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and
(b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and ....
Run Code Online (Sandbox Code Playgroud)
你可以使用这样的东西:
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()]));
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
当然,您也可以使用另一种没有动态列表的方法:
对于:
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));
Run Code Online (Sandbox Code Playgroud)
你可以用这个:
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();
Run Code Online (Sandbox Code Playgroud)
如您所见,如何在运行时使用greendao组合WhereConditions有很多可能性.但只要您没有详细说明您真正想做的事情,就没有人可以帮助您.
顺便说说:
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 '%'+condition1或a like condition1+'%'匹配的结果a like '%'+condition1+'%'.| 归档时间: |
|
| 查看次数: |
6038 次 |
| 最近记录: |