Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select distinct values from ORMlite?

I want to get distinct values from Database. Currently I am using ORMlite. I have tried the following code. But still I am getting repeated values from, when I am querying. Could anyone point out where I have gone wrong? I am posting the sample code below

smsDao = databaseHelper.getSmsDao();
    QueryBuilder<SMSObject, Object> queryBuilder = smsDao.queryBuilder();
    queryBuilder.distinct().selectColumns("timestamp");

    try {
        preparedQuery = queryBuilder.prepare();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        smsObject = smsDao.query(preparedQuery);
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

Any help is appreciable.

like image 691
Sanal Varghese Avatar asked Nov 11 '22 21:11

Sanal Varghese


1 Answers

 dataList = Utils.getDBHelper(ItemList.this).getItemDao().queryBuilder().where().in("nameid", lis).query();     

This is the code I used earlier in my app. It was retrieving duplicate values from the database. Then I updated it with the distinct() method and everything started to work fine.

 dataList = Utils.getDBHelper(ItemList.this).getItemDao().queryBuilder().distinct().where().in("nameid", lis).query();

Note: Here 'lis' is the iteratable list in which my search parameters are added. If you don't have one, you can use 'eq()' instead of 'in()'.

like image 134
poly Avatar answered Nov 14 '22 23:11

poly