2011-09-30 72 views
0

我正在寫在ormlite查詢如下如何在使用for循環

Where<Advertisement, Integer> where = queryBuilder.where(); 
where.and(
    where.between("latitude", pLatitude - APPOXIMATION_FACTOR, 
     pLatitude + APPOXIMATION_FACTOR), 
    where.between("longitude", pLongitude - APPOXIMATION_FACTOR, 
     pLongitude + APPOXIMATION_FACTOR) 
     .and().between("width", pWidth - APPOXIMATION_FACTOR, 
     pWidth + APPOXIMATION_FACTOR), 
); 

,也多了一個與此

for (int iterator = 0; iterator < moduleList.size(); iterator++) { 
    where.eq("id", moduleList.get(iterator).getmId()); 
    if (iterator != advertisementList.size() - 1){ 
     whereForModuleID.or(); 
    } 
} 

,但我的條件下產生寫在ormlite查詢卡如何編寫查詢在這種情況下

尋找幫助

+0

難道我的回答幫助@deepak? – Gray

回答

7

在我,而不是使用第二種情況where.in(String, Iterable)方法。你應該做這樣的事情:

List<Integer> idList = new ArrayList<Integer>(); 
for (Module module : moduleList) { 
    idList.add(module.getmId()); 
} 
where.in("id", idList); 

這變成了一個SQL查詢,如:

SELECT * `foo` WHERE `id` IN (7, 17, 1, 34) 

這裏是文檔上where.in()

http://ormlite.com/docs/where-in

在原始問題的條款,請參閱此answer about the where.and(int) and or(int) methods

+0

thnx灰色它的作品像魅力.... –

0

要創建一個查詢,看起來一個帳戶名稱和密碼,您將執行以下操作:

QueryBuilder<Account, String> qb = accountDao.queryBuilder(); 
Where where = qb.where(); 
// the name field must be equal to "foo" 
where.eq(Account.NAME_FIELD_NAME, "foo"); 
// and 
where.and(); 
// the password field must be equal to "_secret" 
where.eq(Account.PASSWORD_FIELD_NAME, "_secret"); 
PreparedQuery<Account, String> preparedQuery = qb.prepareQuery(); 

這是我的代碼,我用我的項目使用ORMLite

從SQLite的獲取PropertyModel
public ArrayList<PropertyPicModel> selectArgumentQueryPropertyModel(int property_id, Dao<PropertyPicModel, Integer> dao) 
{ 
    try { 
     QueryBuilder<PropertyPicModel, Integer> queryBuilder = dao.queryBuilder(); 
       Where<PropertyPicModel, Integer> where = queryBuilder.where(); 
       SelectArg selectArg = new SelectArg(); 
       // define our query as 'property_id = ?' 

       where.eq(ORMLiteConfig.PROPERTY_ID, selectArg); 
       // prepare it so it is ready for later query or iterator calls 
       PreparedQuery<PropertyPicModel> preparedQuery = queryBuilder.prepare(); 
       selectArg.setValue(property_id); 
       // later we can set the select argument and issue the query 

       ArrayList<PropertyPicModel> picList = (ArrayList<PropertyPicModel>) dao.query(preparedQuery); 
       return picList; 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     MyLog.e("Excep selectArgumentQuery " +e.toString()); 
    } 
    catch (Exception e) { 
     MyLog.e("Excep selectArgumentQuery " +e.toString()); 
    } 
    return null; 
} 

在這個例子中,將要生成的SQL查詢將約爲:

SELECT * FROM account WHERE (name = 'foo' AND passwd = '_secret') 

https://github.com/AshishPsaini/ormlite-examples/tree/master/android/HelloAndroid

http://ormlite.com/docs/in