8

I have a List of Strings each String is a unique identifier for a item persisted through GreenDao.

How do I build a query that allows me to load all this items form my database?

Is there a possibility to do it with a QueryBuilder or do I need to go back to writing SQL?

Janusz
  • 187,060
  • 113
  • 301
  • 369

2 Answers2

7

This is possible with the in condition in the Property class.

This example loads all boxes with field values contained in fieldValues. fieldValues is of the type List<String>

  List<LocalBox> boxes = getBoxDao(context).queryBuilder()
        .where(LocalBoxDao.Properties.field.in(fieldValues)).list();
Janusz
  • 187,060
  • 113
  • 301
  • 369
2

I would like to put some light in "in query",
There is a limitation in this, we cant pass more than 100 ids inside a in query
So what I did to achieve this is:

List<Product> productList = new ArrayList<Product>();
DaoSession daoSessionUni = TarneaAndroidApplicationContext.getInstance().getDaoSession();

for (int i = 0; i < rowIds.size(); i = i + 100) 
{
    ProductDao productDao = daoSessionUni.getProductDao();
    QueryBuilder<Product> queryBuilder = productDao.queryBuilder().where(
        ProductDao.Properties.Id.in(rowIds.subList(i + 100 < rowIds.size() ? 
                                                   i + 100 : 
                                                   rowIds.size())),
        ProductDao.Properties.IsDeleted.eq(0));             
    productList.addAll(queryBuilder.list());
}

if we want to pass a list of id using in Query we can use this method.

Artem Shmatkov
  • 1,434
  • 5
  • 22
  • 41