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

发布于 2024-12-07 01:59:42 字数 742 浏览 0 评论 0原文

我正在 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();
    }
}

,但我无法在这种情况下编写查询

寻求帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-12-14 01:59:42

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

就原始问题而言,请参阅此有关where.and(int)or(int) 方法

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.

棒棒糖 2024-12-14 01:59:42

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

 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文