簡體   English   中英

從Sqlite android獲取值時出錯

[英]Getting error while fetching value from Sqlite android

我只能獲取第一行值,而不能獲取其他值:

使用Stetho輸出

選擇一天,血壓詳細信息按天平均(收縮)

日平均(收縮壓)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;

    }
}

日志貓:-

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) 

我得到了解決方案:

它應該是

System.out.println(“輸出為” + cursor.getFloat(cursor.getColumnIndex(“ avg(” + columnName +“)”))));;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM