How to write query in ormlite when the condition generated using for loop
I'm writing a query in ormlite as below
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),
);
and also one more and with this
for (int iterator = 0; iterator < moduleList.size(); iterator++) {
where.eq("id", moduleList.get(iterator).getmId());
if (iterator != advertisementList.size() - 1){
whereForModuleID.or();
}
}
but i am stuck how to write query in this case
Looking for help
In the 2nd case I'd use instead the where.in(String, Iterable) method. You should do something like this:
List<Integer> idList = new ArrayList<Integer>();
for (Module module : moduleList) {
idList.add(module.getmId());
}
where.in("id", idList);
This turns into a SQL query like:
SELECT * `foo` WHERE `id` IN (7, 17, 1, 34)
Here are the docs on where.in()
:
http://ormlite.com/docs/where-in
In terms of the original question, see this answer about the where.and(int)
and or(int)
methods.
To create a query which looks up an account by name and password you would do the following:
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();
This is my code which i am using in my project to get PropertyModel from SQLite using ORMLite
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;
}
In this example, the SQL query that will be generated will be approximately:
SELECT * FROM account WHERE (name = 'foo' AND passwd = '_secret')
https://github.com/AshishPsaini/ormlite-examples/tree/master/android/HelloAndroid
http://ormlite.com/docs/in
精彩评论