错误字段0,-1的错误请求

时间:2012-03-20 06:27:31

标签: android sqlite error-handling

我正在研究一个Android数据库应用程序,并且已经解雇了这样的查询

public Cursor strok(String holid, String gameId2) throws SQLException  {
            // TODO Auto-generated method stub

            try{



                abc = db.rawQuery("select MAX(strokeNo) from extended where "
                         + " holeId = " + holid + "  and " + " courseId = " + gameId2+";"
                          ,null);

                    }

                catch(Exception e)

                       {

                          Log.e("ERROR OF HOLE ID IS ",  e.toString());
                           e.printStackTrace();

                        }

                        System.out.println("hello " + abc);

                return abc;
        }

并在此活动中接收

  Cursor cursor = db.strok(HoleIds.get(n),gameId);

                    System.out.println("after query ");

                    cursor.moveToFirst();

                startManagingCursor(cursor);

                System.out.println("the recived value is " +cursor);

        String maxs = cursor.getString(cursor.getColumnIndex("strokeNo"));
                    System.out.println("the recived value is " +maxs);

but it is giving an error which i have not been able to find it out

日志猫是

03-20 11:43:43.396: E/CursorWindow(6138): Bad request for field slot 0,-1. numRows = 1, numColumns = 1
03-20 11:43:43.396: E/ERROR OF HOLE ID IS(6138): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
03-20 11:43:43.396: W/System.err(6138): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
03-20 11:43:43.526: W/System.err(6138):     at android.database.CursorWindow.getString_native(Native Method)
03-20 11:43:43.526: W/System.err(6138):     at android.database.CursorWindow.getString(CursorWindow.java:329)
03-20 11:43:43.526: W/System.err(6138):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
03-20 11:43:43.526: W/System.err(6138):     at golf.score.project.ExtendedScoreDisplay.onCreate(ExtendedScoreDisplay.java:123)
03-20 11:43:43.536: W/System.err(6138):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-20 11:43:43.556: W/System.err(6138):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-20 11:43:43.556: W/System.err(6138):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-20 11:43:43.556: W/System.err(6138):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-20 11:43:43.556: W/System.err(6138):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-20 11:43:43.567: W/System.err(6138):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 11:43:43.577: W/System.err(6138):     at android.os.Looper.loop(Looper.java:123)
03-20 11:43:43.598: W/System.err(6138):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-20 11:43:43.598: W/System.err(6138):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 11:43:43.606: W/System.err(6138):     at java.lang.reflect.Method.invoke(Method.java:521)
03-20 11:43:43.606: W/System.err(6138):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-20 11:43:43.606: W/System.err(6138):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-20 11:43:43.606: W/System.err(6138):     at dalvik.system.NativeStart.main(Native Method)

我想知道错误的位置以及如何将其删除

2 个答案:

答案 0 :(得分:2)

 abc = db.rawQuery("select MAX(strokeNo) from extended where "
                         + " holeId = " + holid + "  and " + " courseId = " + gameId2+";"
                          ,null);

当谈到Sql中的where子句时,你应该知道String必须被'。

包围
abc = db.rawQuery("select MAX(strokeNo) from extended where "
                         + " holeId = '" + holid + "'  and " + " courseId = '" + gameId2+"';"
                          ,null);

编辑:

在sql查询中,您只选择了一个字段,因此光标只有一个字段

String maxs = cursor.getString(0);

答案 1 :(得分:0)

我想我在你的代码中发现了问题。您正在使用

abc = db.rawQuery("select MAX(strokeNo) from extended where " + " holeId = " + holid + " and " + " courseId = " + gameId2+";" ,null); }

并使用 cursor.getString(cursor.getColumnIndex("strokeNo")) 检索值。但是在结果游标中没有这样的列strokeNo

您应该使用cursor.getString(0);代替。这应该可以正常工作。

还有另一种解决方案。您可以将查询更改为

abc = db.rawQuery("select MAX(strokeNo) as maxstroke from extended where " + " holeId = " + holid + " and " + " courseId = " + gameId2+";" ,null); }

并在检索时使用cursor.getString(cursor.getColumnIndex("maxstroke"))。我认为这将解决您的问题。