How do I execute “select distinct ename from emp” using GreenDao

我只是一个虾纸丫 提交于 2019-12-09 03:13:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23445174/how-do-i-execute-select-distinct-ename-from-emp-using-greendao

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!