Data deduplication for greendao query

In the process of using greendao recently, there is a requirement: to display the contents of the database according to groups. It means that all groups in the database need to be taken out, and then the data is loaded according to the group. Before, my stupid way was to get all the data, and then manually deduplicate the obtained data (compare the group values ​​of each entity to see if they are consistent, or add them to a List collection). 
The stupid method will not have any effect in a relatively small number of databases, but in pursuit of perfection, I queried the database and found that I need the "SELECT DISTINCT" field to query, but I don't know SQLite, how can I query this? It's very close to success at this time, but I'm still lazy - go directly to find out how others achieved it?

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;
}

This method can be achieved directly, but the DaoSession object is not easy to find, it is an object automatically generated by greendao, and then adding the getDaoSession() method in EmpDao is invalid, and the manually added method is deleted as soon as it is compiled. I found it in my own GreenDaoHelper method, the code is as follows:

/**
 * Support classes for GreenDao multiple databases
 * Created by Administrator on 2017/4/4 0004.
 */

public class GreenDaoHelper {


    private HashMap<String,DaoSession> hash = new HashMap<String,DaoSession>();

    public String pBaseDbPath = "/PuCha2.0/PestGeneralSurvey/PuChaSurvey.db";


    private Context pContext;


    public GreenDaoHelper(Context pContex,String pBaseDbPath){
        this.pContext = pContex;
        this.pBaseDbPath = pBaseDbPath;
        initDatabase(pBaseDbPath);
    }

    /**
     * Initialize greenDao, this operation is recommended to be added when the Application is initialized;
     */ 
    public   DaoSession initDatabase(String pPath) {
         // Through DaoMaster's inner class DevOpenHelper, you can get a convenient SQLiteOpenHelper object.
        // As you may have noticed, you don't need to write SQL statements like "CREATE TABLE" because greenDAO has already done it for you.
        // Note: The default DaoMaster.DevOpenHelper will delete all tables when the database is upgraded, which means that this will result in data loss.
        // So, in a formal project, you should also do a layer of encapsulation to realize the security upgrade of the database. 
        DaoMaster.DevOpenHelper mHelper = new DaoMaster.DevOpenHelper(pContext, FormUtil.getInnerSDCardPath()+pPath, null );
        SQLiteDatabase db = mHelper.getWritableDatabase();
         // Note: This database connection belongs to DaoMaster, so multiple Sessions refer to the same database connection. 
        DaoMaster mDaoMaster = new DaoMaster(db);
        DaoSession mDaoSession = mDaoMaster.newSession();
        hash.put(pPath,mDaoSession);
        return mDaoSession;
    }

    public DaoSession getDaoSession(String pDbPath) throws FileNotFoundException {
        DaoSession mDaoSession =  hash.get(pDbPath);

        if(!fileIsExists(FormUtil.getInnerSDCardPath()+pDbPath)){
            throw  new FileNotFoundException();
        }

        if(mDaoSession == null){
            return  initDatabase(pDbPath);
        }
        return mDaoSession;
    }

    public DaoSession getBaseDaoSession(){
        DaoSession mDaoSession =  hash.get(pBaseDbPath);
        if(mDaoSession == null){
            return  initDatabase(pBaseDbPath);
        }
        return  mDaoSession;
    }

    public boolean fileIsExists(String pPath){
        try{
            File f=new File(pPath);
            if(!f.exists()){
                return false;
            }
        }catch (Exception e) {
            // TODO: handle exception
            return false;
        }
        return true;
    }

}

method source

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809778&siteId=291194637