首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Sqlite android抓取值时出错

从Sqlite android抓取值时出错
EN

Stack Overflow用户
提问于 2016-10-07 11:59:10
回答 1查看 118关注 0票数 0

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

使用Stetho输出

从按天分组的血压详情中选择天、平均(收缩期)

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

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

下面是代码

代码语言:javascript
运行
复制
 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;

    }
}

日志猫:-

代码语言:javascript
运行
复制
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) 
EN

回答 1

Stack Overflow用户

发布于 2016-10-07 13:17:59

我得到了解决方案:

它应该是

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39909148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档