javaandroidgreendao

How to use OR queries in greenDAO?


I have a query like:

Select * from table where user = 'user1' or city = 'delhi';

I know how to do it for single user but I am not getting how can I use or in following query.

dao.queryBuilder()
    .where(UserDao.Properties.UserId.eq(userId1))
    .list();

Solution

  • For Version 3.2.+, here is an example to use a combination of Where() and WhereOr() conditions. The below is a hypothetical query to select all items:

    1. That have the tags 'paint', 'emulsion'
    2. That belong to a specific Category
    3. Excluding a particular Sub Category

    The Where() method takes exactly one query condition and the WhereOr() can take multiple query conditions (as many as the number of properties in the Dao Class), separated by comma

    String catgId = "AB12545";
    String excludeSubCatgId = "SAB09990";
    DaoSession daoSession = ((App) getApplication()).getDaoSession();
    
    List<Item> = daoSession.getItemDao().queryBuilder()
            .where(ItemDao.Properties.CategoryId.eq(catgId))
            .where(ItemDao.Properties.SubCategory.notEq(excludeSubCatgId))
            .whereOr(ItemDao.Properties.ItemTagCloud.like("%paint%"),
                     ItemDao.Properties.ItemTagCloud.like("%emulsion%"))
            .orderDesc(ItemDao.Properties.ItemPrice)
            .list();