简体   繁体   中英

Getting error while fetching value from Sqlite android

I am able to fetch only first row values not other:

Output using the Stetho

select day,avg(systolic) from bloodpressuredetails group by day

Day avg(systolic) 2 132.5 1 126.625 0 123.0

I am getting only last value ie 123.0 not others.

Below Is the code

 public ArrayList<Float> fetchValuesFromBp(String columnName,int fetchedDays)
    {
        //fetched Days means how many days data has to be fetch.

        SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();

        ArrayList<Float> arrayList=new ArrayList<Float>();
        Cursor cursor=sqLiteDatabase.rawQuery("select avg("+columnName+") from "+bpTableName+" group by day",null);

        cursor.moveToFirst();
        System.out.println("Length is "+cursor.getCount());


        int i=0;

        if (cursor.moveToFirst())
        {
            while (!cursor.isAfterLast())
            {
                System.out.println("Output is "+cursor.getFloat(i));
                arrayList.add(cursor.getFloat(i));

                i++;
                cursor.moveToNext();
            }
        }
        cursor.close();

        return arrayList;

    }
}

Log Cat:-

10-07 09:08:22.157 31839-31839/user.com.test2 I/System.out: Length is 3
10-07 09:08:22.157 31839-31839/user.com.test2 I/System.out: OutPut is 123.0
10-07 09:08:22.158 31839-31839/user.com.test2 E/CursorWindow: Failed to read row 1, column 1 from a CursorWindow which has 3 rows, 1 columns.
10-07 09:08:22.159 31839-31839/user.com.test2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: user.com.test2, PID: 31839
    java.lang.RuntimeException: Unable to start activity ComponentInfo{user.com.test2/user.com.hlthee.BpGraph}: java.lang.IllegalStateException: Couldn't read row 1, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: Couldn't read row 1, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetDouble(Native Method)
        at android.database.CursorWindow.getDouble(CursorWindow.java:543)
        at android.database.CursorWindow.getFloat(CursorWindow.java:594)
        at android.database.AbstractWindowedCursor.getFloat(AbstractWindowedCursor.java:81)
        at Helper.BloodPressureDatabase.fetchValuesFromBp(BloodPressureDatabase.java:137)
        at user.com.hlthee.BpGraph.onCreate(BpGraph.java:68)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I got the solution:

it should be

System.out.println("Output is "+cursor.getFloat(cursor.getColumnIndex("avg("+columnName+")")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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