2013-05-16 80 views
0

我需要添加一些or子句進行查詢。我需要循環做。GreenDao where或條件加入循環

StringTokenizer st = new StringTokenizer(symptoms, ","); 
while(st.hasMoreTokens()){ 
    qb.whereOr(Properties.Symptom.like("%" + st.nextToken() + "%")); 
} 

我怎樣才能正確地添加這些or條件,因爲這上面無法按預期工作。我想爲每個症狀添加or

回答

2

If you look at the documentation,您會看到whereOr()接受無限數量的條件。你想要做的是一次在數組中添加他們都:

StringTokenizer st = new StringTokenizer(symptoms, ","); 

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

while(st.hasMoreTokens()){ 
    whereConditions.add(Properties.Symptom.like("%" + st.nextToken() + "%")); 
} 

// Give the ArrayList an already allocated array to place its contents in. 
WhereCondition[] conditionsArray = new WhereCondition[whereConditions.size()]; 
conditionsArray = whereConditions.toArray(conditionsArray); 

qb.whereOr(conditionsArray); 

它看起來像文檔中調用該方法需要兩個非陣列WhereConditions然後一個ellipsized參數,它接受一個數組或附加逗號分隔的對象列表。所以,你可能需要做這樣的事情來得到它才能正常工作:

qb.whereOr(conditionsArray[0], conditionsArray[1], Arrays.copyOfRange(conditionsArray, 2, conditionsArray.length); 

附錄:它看起來像你使用不文檔,可能是舊版本greenDAO匹配的API。我根據當前的文檔編寫了這個解決方案。我不能保證它會爲你工作。如果可能,我建議更新。

0

試試這個:

StringTokenizer st = new StringTokenizer(symptoms, ","); 
WhereCondition where = null; 
while(st.hasMoreTokens()){ 
    if (where != null) { 
     where = qb.or(where, Properties.Symptom.like("%" + st.nextToken() + "%")); 
    } else { 
     where = Properties.Symptom.like("%" + st.nextToken() + "%"); 
    } 
} 
qb.where(where).list(); 
0

我有同樣的問題,所以我說在的Util類我自己的方法時,我有一個或一個數組的幾個WhereCondition執行相同的行爲。

這裏是我的網關方法:

public static QueryBuilder whereOr(QueryBuilder queryBuilder, WhereCondition[] whereConditions){ 
     if(whereConditions == null) return queryBuilder.where(null); 
     else if(whereConditions.length == 1) return queryBuilder.where(whereConditions[0]); 
     else return queryBuilder.whereOr(whereConditions[0], whereConditions[1], Arrays.copyOfRange(whereConditions, 2, whereConditions.length)); 
    } 

用途:Util.whereOr(queryBuilder, whereConditionsArray);

默認:不能使用從QueryBuilder的Builder模式這種方法

(更多更高版本)在這裏,我與你分享一些代碼,可以在開發DAO方法時省下時間。

public class QueryBuilderUtil { 
    public static final String EQ = "=?"; 
    public static final String NOTEQ = "<>?"; 
    public static final String LIKE = " LIKE ?"; 
    public static final String GE = ">=?"; 
    public static final String LE = "<=?"; 
    public static final String GT = ">?"; 
    public static final String LT = "<?"; 


    public static QueryBuilder whereOrOnSamePropertyWithDifferentValues(QueryBuilder queryBuilder, Property property, String operation, String values, String separator) { 
     return whereOrOnSamePropertyWithDifferentValues(queryBuilder, property, operation, values.split(separator)); 

    } 

    public static QueryBuilder whereOrOnSamePropertyWithDifferentValues(QueryBuilder queryBuilder, Property property, String operation, String[] values) { 
     WhereCondition[] whereConditions = new WhereCondition[values.length]; 
     int i = 0; 
     for (String value : values) { 
      whereConditions[i++] = new WhereCondition.PropertyCondition(property, operation, value); 
     } 
     return whereOr(queryBuilder, whereConditions); 
    } 

    public static QueryBuilder whereOr(QueryBuilder queryBuilder, WhereCondition[] whereConditions) { 
     if (whereConditions == null) return queryBuilder.where(null); 
     else if (whereConditions.length == 1) return queryBuilder.where(whereConditions[0]); 
     else return queryBuilder.whereOr(whereConditions[0], whereConditions[1], Arrays.copyOfRange(whereConditions, 2, whereConditions.length)); 
    } 
} 

使用此類,您可以在一行中對多個「值字符串」執行具有相同屬性的whereOr。有必要清理我的代碼:)。但是,您只能執行簡單的操作,如類中聲明的變量。

例子:

public List<Block> loadAllByModId(String mods_id) { 
     synchronized (this) { 
      QueryBuilder<Block> queryBuilder = queryBuilder(); 
      QueryBuilderUtil.whereOrOnSamePropertyWithDifferentValues(queryBuilder, Properties.ModId, QueryBuilderUtil.EQ, mods_id, ";"); 
      query_list = queryBuilder.build(); 
     } 
     Query<Block> query = query_list.forCurrentThread(); 
     return query.list(); 
    } 

希望它可以幫助