2016-09-05 6 views
2

특정 조건을 기반으로 쿼리를 작성하는 방법. 쿼리 빌더 dbflow 조건부 쿼리

내가이

QueryBuilder builder = SQlite.Select().from(Table) 
    if(condition) { 
     builder.where(something) 
    } 
Cursor c = builder.query; 

일을 시도했지만이 허용되지 않습니다.

환경 설정에서 저장 한 조건에 대해 데이터베이스를 쿼리해야합니다. 나는 인터넷 검색을 시도하고 도처에서 문서를 검색했지만 단일 예제를 찾을 수 없었다. 이 기능은 dbflow에 존재합니까? 그렇다면 어떻게하면이 기능이있는 다른 orm (greenDAO와 같은)이 있습니까?

답변

2

DBFlow에서 조건부 쿼리를 작성하십시오. 테이블의 열을 쿼리하려면 클래스 이름에 _Table을 추가 한 다음 속성에 액세스해야합니다. 이러한 _Table 클래스는 빌드하는 동안 생성됩니다.

가장 간단한 쿼리는 것이 하나

SQLite.select() 
     .from(YourTable.class) 
     .where(YourTable_Table.id.eq(someId) 
     .queryList(); 

또한 쿼리에 .and.or를 사용하여 새로운 조건을 추가 할 수 있습니다 : 당신은 또한 수, 깨끗한 코드를

SQLite.select() 
     .from(YourTable.class) 
     .where(YourTable_Table.id.eq(someId) 
     .and(YourTable_Table.name.eq(someName) 
     .queryList(); 

조건을 다음과 같은 조건 그룹으로 그룹화하십시오 :

ConditionGroup conditionGroup = ConditionGroup.clause(); 
conditionGroup.and(YourTable_Table.id.eq(someId); 

if (someCondition) { 
    conditionGroup.and(YourTable_Table.name.eq(someName); 
} 

return SQLite.select() 
     .from(YourTable.class) 
     .where(conditionGroup) 
     .queryList(); 
+0

를 사용하여 내 문제

1.from @trevjonez (트레버 존스)

Where<SomeModel> query = SQLite.select() .from(SomeModel.class) .where(SomeModel_Table.date_field.greaterThan(someValue)); if(someCondition) { query = query.and(SomeModel_Table.other_field.eq(someOtherValue)); } else { query = query.and(SomeModel_Table.another_field.isNotNull()); } Cursor cursor = query.query(); //do stuff with cursor and close it — 

2.from의 @zshock를 달성하는 두 가지 방법을 발견 ... .thnks –

2

ConditionalGroup 내가 없어진 것입니다 ConditionalGroup

ConditionGroup conditionGroup = ConditionGroup.clause(); 
conditionGroup.and(YourTable_Table.id.eq(someId); 

if (someCondition) { 
    conditionGroup.and(YourTable_Table.name.eq(someName); 
} 

return SQLite.select() 
     .from(YourTable.class) 
     .where(conditionGroup) 
     .queryList();