Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute "select distinct ename from emp" using GreenDao

Tags:

greendao

How do I execute "select distinct ename from emp" using GreenDao

I am trying to get distinct values of a column of sqlite DB using GreenDao. How do I do it? Any help appreciated.

like image 544
Ananth Avatar asked May 03 '14 13:05

Ananth


People also ask

How do you use distinct in the middle of a selected statement?

The DISTINCT clause filters out FULL DUPLICATE ROWS. It goes right after the SELECT keyword, since it applies to the entire row, not single columns. You cannot use it in between columns.

Is select distinct fast?

Most of the SELECT DISTINCT queries will perform exactly as fast as their simple SELECT counterparts, because the optimizer will do away with the step necessary for eliminating duplicates.


1 Answers

You have to use a raw query for example like this:

private static final String SQL_DISTINCT_ENAME = "SELECT DISTINCT "+EmpDao.Properties.EName.columnName+" FROM "+EmpDao.TABLENAME;

public static List<String> listEName(DaoSession session) {
    ArrayList<String> result = new ArrayList<String>();
    Cursor c = session.getDatabase().rawQuery(SQL_DISTINCT_ENAME, null);
    try{
        if (c.moveToFirst()) {
            do {
                result.add(c.getString(0));
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return result;
}

Of course you can add some filter-criteria to the query as well.

The static String SQL_DISTINCT_ENAME is used for performance, so that the query string doesn't have to be built every time.

EmpDao.Properties and EmpDao.TABLENAME is used to always have the exact column-names and table-names as they are generated by greendao.

like image 142
AlexS Avatar answered Sep 21 '22 04:09

AlexS