QUery将光标移出绑定的异常

时间:2013-10-23 06:29:07

标签: sqlite

查询 - 获取param_name和param_value时,它给出了绑定异常的索引 - 当它正常工作直到昨天它突然开始给出错误

Cursor cur = db
            .rawQuery("SELECT SP.PARAM_NAME AS PARAM_NAME, SRD.PARAM_VALUE AS PARAM_VALUE FROM QT_UM_SHED_READY SR, "
                    +" QT_UM_SHED_READY_DTL SRD LEFT OUTER JOIN QM_UM_SHED_PARAM SP "
                    +" ON SP.PARAM_ID = SRD.PARAM_ID WHERE SR.MOB_ID=SRD.MOB_ID "
                    +" AND SR.MEMBER_ID" + "=? " + "AND SR.ENTRY_DATE = (SELECT MAX (ENTRY_DATE) FROM QT_UM_SHED_READY)" , new String[] { memberId });


    String param_Name = cur.getString(cur.getColumnIndex("PARAM_NAME"));
    String param_Value = cur.getString(cur.getColumnIndex("PARAM_VALUE"));

logcat的

10-23 06:20:54.891: W/dalvikvm(1121): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-23 06:20:55.071: E/AndroidRuntime(1121): FATAL EXCEPTION: main
10-23 06:20:55.071: E/AndroidRuntime(1121): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manacle.umma.activity/com.manacle.umma.activity.ShedReports}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.os.Looper.loop(Looper.java:137)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at dalvik.system.NativeStart.main(Native Method)
10-23 06:20:55.071: E/AndroidRuntime(1121): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at com.manacle.umma.activity.HeaderDb.getShedRep(HeaderDb.java:4145)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at com.manacle.umma.activity.ShedReports.onCreate(ShedReports.java:95)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.Activity.performCreate(Activity.java:5104)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-23 06:20:55.071: E/AndroidRuntime(1121):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-23 06:20:55.071: E/AndroidRuntime(1121):     ... 11 more

1 个答案:

答案 0 :(得分:0)

如果您还没有有效记录,则无法获取列索引;您需要先调用类似moveToFirstmoveToNext的内容:

Cursor cur = db.rawQuery(...);

// for multiple records:
while (cur.moveToNext()) {
    String param_Name = cur.getString(cur.getColumnIndex("PARAM_NAME"));
    String param_Value = cur.getString(cur.getColumnIndex("PARAM_VALUE"));
    ...
}

// alternatively, for a single record:
if (cur.moveToFirst()) {
    String param_Name = cur.getString(cur.getColumnIndex("PARAM_NAME"));
    String param_Value = cur.getString(cur.getColumnIndex("PARAM_VALUE"));
}