使用for循环生成条件时如何在ormlite中编写查询

Posted

技术标签:

【中文标题】使用for循环生成条件时如何在ormlite中编写查询【英文标题】:How to write query in ormlite when the condition generated using for loop 【发布时间】:2011-09-30 03:44:19 【问题描述】:

我正在用 ormlite 编写如下查询

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();
    

但我被困在这种情况下如何编写查询

寻求帮助

【问题讨论】:

【参考方案1】:

在第二种情况下,我会改用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。

【讨论】:

【参考方案2】:

要创建一个通过名称和密码查找帐户的查询,您需要执行以下操作:

 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

【讨论】:

以上是关于使用for循环生成条件时如何在ormlite中编写查询的主要内容,如果未能解决你的问题,请参考以下文章

使用条件 while/for 循环的指针在编译时会出错

JAVA的for语句结束循环条件中如何使用==符号?

如何在 javascript 脚本标签中使用 razor for 循环?

如何修改 ServiceStack.OrmLite 生成的 SQL?

如何在C ++中构造一个for循环复合?

Linux | shell脚本-条件判断if和循环语句for