如何使用GreenDAO全部选择?

时间:2016-06-26 19:08:26

标签: java sqlite android-studio orm greendao

有谁知道如何在greenDAO中执行简单的select * from table并将其放入实体中?我对此做了一些研究,我无法得到任何简单的例子。这就是我到目前为止所做的:

public void storeAppTimeUsageData(AppTimeUsage stats) {
    List<AppTimeUsage> items = new ArrayList<>();
    //appTimeUsageDao = DeviceInsightApp.getSession(this, true).getAppTimeUsageDao();
    try {
        // master
        appTimeUsageDao.insertOrReplace(stats);
    //} catch (IOException e) {
    } catch (Exception e) {
        Log.e("Error", "Some exception occurred", e);
        Log.e("APP_TAG", "STACKTRACE");
        Log.e("APP_TAG", Log.getStackTraceString(e));
    }
    String sql = "SELECT * FROM APP_TIME_USAGE ";
    Cursor c = appTimeUsageDao.getDatabase().rawQuery(sql, null);
    int offset = 0;
    int d ;
    int cd ;
    String e = "";
    while (c.moveToNext()) {
        AppTimeUsage atu AppTimeUsage(
            c.getLong(0);
            //long b =   c.getInt(0);
            d = c.getInt(2);
            e = c.getString(3);
            break;
        );
        items.add(atu);
    }
} 

1 个答案:

答案 0 :(得分:5)

GreenDAO已经内置了实现此任务的方法。在你的情况下:

List<AppTimeUsage> items = appTimeUsageDao.loadAll();

这将从APP_TIME_USAGE中选择所有记录并返回包含实体的List<AppTimeUsage>