Search code examples
javasqliteandroid-studioormgreendao

How to do a select all using GreenDAO?


Does anyone know how to do a simple select * from table in greenDAO and place it into an entity? I have done some research on this and I am unable to get any simple example. This is what I have so far:

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

Solution

  • GreenDAO already comes with a built-in method for achieve this task. In your case:

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

    This will select all records from APP_TIME_USAGE and return a List<AppTimeUsage> containing the entities.