Question

I need to return only a single column using GreenDao. This is the SQL query:

SELECT NAME FROM PERSON

How can I do it using GreenDao?

No correct solution

OTHER TIPS

Well greenDao does provide access to the underlying database if you want to perform raw queries (DaoSession.getDatabase) - which you may feel in this case would be more efficient than the greenDao alternative - which would involve getting all Persons and iterating over the results to extract the name (easily done with personDao.loadAll()). The idea in using an ORM library would be to use Person objects, and access the name attribute on it. e.g. person.getName().

public static ArrayList<ArrayList<String>> rawQuery(final String query) {
        SQLiteDatabase db = DBHelper.getDaoSession().getDatabase();
        Cursor cursor = db.rawQuery(query, null);
        retList = new ArrayList<ArrayList<String>>();
        ArrayList<String> list = new ArrayList<String>();
        if (cursor.moveToFirst()) {
            do {
                list = new ArrayList<String>();
                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    list.add(cursor.getString(i));
                }
                retList.add(list);
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        return retList;

    }

Use this kudos let me know you want something more ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top