简体   繁体   English

GreenDao选择单列?

[英]GreenDao select a single column?

I need to return only a single column using GreenDao. 我需要使用GreenDao返回一个列。 This is the SQL query: 这是SQL查询:

SELECT NAME FROM PERSON

How can I do it using GreenDao? 我怎么能用GreenDao做到这一点?

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()). 如果你想执行原始查询(DaoSession.getDatabase),greenDao确实提供对底层数据库的访问 - 在这种情况下你可能会觉得比greenDao替代方案更有效 - 这将涉及获取所有人并迭代结果到提取名称(使用personDao.loadAll()轻松完成)。 The idea in using an ORM library would be to use Person objects, and access the name attribute on it. 使用ORM库的想法是使用Person对象,并访问其上的name属性。 eg person.getName(). 例如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 ? 使用这个荣誉让我知道你想要更多的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM