从Sqlite android

时间:2016-10-07 03:59:10

标签: java android sqlite fetch

我只能获取第一行而不是其他值:

使用Stetho输出

从白天选择daypress,avg(收缩压)来自bloodpressuredetails组

日平均(收缩期) 2 132.5 1 126.625 0 123.0

我只得到最后一个值,即123.0而不是其他值。

以下是代码

 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) 

1 个答案:

答案 0 :(得分:0)

我得到了解决方案:

应该是

System.out.println(&#34;输出为&#34; + cursor.getFloat(cursor.getColumnIndex(&#34; avg(&#34; + columnName +&#34;)&#34; )));