QueryBuilder.and()
및 은 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+'%' 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()]));
}
UPDATE :
(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and
(b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and ....
이 같은 것을 사용할 수 있습니다 :
01 대한 23,516,:
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 사용하여 런타임에 WhereConditions을 결합 할 수있는 방법 possiblities 많이있다시피. 그러나 당신이 정말로하고 싶은 것을 상세하게 예시하고 설명하지 않는 한 아무도 당신을 도울 수 없습니다. 그런데
: 당신은 하나의 WhereCondition을 사용하는 경우, 어떤 차이가되지 않습니다 where()
또는 whereOr()
를 사용
- .
- 아마도
(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%')
대신 (a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%')
을 쿼리하고 싶을 것입니다.
- 통지, 그
(a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%')
는 a like '%'+condition1
또는 a like condition1+'%'
도 a like '%'+condition1+'%'
일치합니다 일치하는 모든 결과 이후 (a like '%'+condition1+'%')
동일합니다.
이것은 완전히 새로운 질문이며 greendao와 관련이없는 것 같습니다. 또한 소스 코드가 명확하지 않습니다. 문제를 일으키는 명확한 소스 코드를 제공하고 소스 코드 란 무엇이며 로그 출력은 무엇이며 텍스트는 무엇인지 분명히하십시오. – AlexS