Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I build a GreenDao query that loads all Items specified through a list of ids?

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?

like image 242
Janusz Avatar asked Jan 07 '13 11:01

Janusz


2 Answers

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();
like image 155
Janusz Avatar answered Oct 21 '22 06:10

Janusz


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.

like image 43
user1935071 Avatar answered Oct 21 '22 04:10

user1935071