Мой sqLite-курсор работает над концом цикла и генерирует исключение CursorIndexOutOfBoundsException - PullRequest
0 голосов
/ 19 мая 2019

Я заполнил базу данных sqLite под Android 561 записью ..
и теперь я пытаюсь записать их в Журнал.

Я разбил его и добавил в начало и в точки останова, но он опускается до конца ...
Google отлично подходит для курсоров с 0 записями ..

        sqLiteDatabase.execSQL("INSERT INTO product (barcode, description, brand) VALUES ('20675578','smoked back bacon - 8 rashers','warren & sons')");
        sqLiteDatabase.execSQL("INSERT INTO product (barcode, description, brand) VALUES ('5010431600941','Meatballs','ye olde oak')");
        Log.i("UserResults - name", "Point 3");

        // List the products
        Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM product", null);
        int barcodeIndex = c.getColumnIndex("barcode");
        int descriptionIndex = c.getColumnIndex("description");
        int brandIndex = c.getColumnIndex("brand");

        c.moveToFirst();
        while (c != null) {
            String textString = "Product - [" + c.getString(barcodeIndex) + "]["+ c.getString(descriptionIndex)+ "][" + c.getString(brandIndex) + "]";
            Log.i("SQL-Log", textString );
            c.moveToNext();
        }
        c.close();

Я ожидаю, что это верхний переход к следующему разделу, который должен загрузить записи в другую таблицу ... но я получаю это:

019-05-19 16:05:29.102 3402-3402/com.example.food001 I/SQL-Log: Product - [20675578][smoked back bacon - 8 rashers][warren & sons]
2019-05-19 16:05:29.102 3402-3402/com.example.food001 I/SQL-Log: Product - [5010431600941][Meatballs][ye olde oak]  <---Good so far!
2019-05-19 16:05:29.104 3402-3402/com.example.food001 W/System.err: android.database.CursorIndexOutOfBoundsException: Index 561 requested, with a size of 561
2019-05-19 16:05:29.105 3402-3402/com.example.food001 W/System.err:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
2019-05-19 16:05:29.105 3402-3402/com.example.food001 W/System.err:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
2019-05-19 16:05:29.106 3402-3402/com.example.food001 W/System.err:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
2019-05-19 16:05:29.106 3402-3402/com.example.food001 W/System.err:     at com.example.food001.MainActivity.onCreate(MainActivity.java:1207)
2019-05-19 16:05:29.106 3402-3402/com.example.food001 W/System.err:     at android.app.Activity.performCreate(Activity.java:7441)
2019-05-19 16:05:29.107 3402-3402/com.example.food001 W/System.err:     at android.app.Activity.performCreate(Activity.java:7431)
2019-05-19 16:05:29.107 3402-3402/com.example.food001 W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
2019-05-19 16:05:29.107 3402-3402/com.example.food001 W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3343)
2019-05-19 16:05:29.107 3402-3402/com.example.food001 W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3548)
2019-05-19 16:05:29.107 3402-3402/com.example.food001 W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
2019-05-19 16:05:29.108 3402-3402/com.example.food001 W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2019-05-19 16:05:29.108 3402-3402/com.example.food001 W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2019-05-19 16:05:29.108 3402-3402/com.example.food001 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2155)
2019-05-19 16:05:29.108 3402-3402/com.example.food001 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:109)
2019-05-19 16:05:29.108 3402-3402/com.example.food001 W/System.err:     at android.os.Looper.loop(Looper.java:207)
2019-05-19 16:05:29.108 3402-3402/com.example.food001 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7539)
2019-05-19 16:05:29.109 3402-3402/com.example.food001 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-05-19 16:05:29.109 3402-3402/com.example.food001 W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
2019-05-19 16:05:29.110 3402-3402/com.example.food001 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
2019-05-19 16:05:29.117 3402-3402/com.example.food001 D/ActivityThread: add activity client record, r= ActivityRecord{a45a309 token=android.os.BinderProxy@7cb25b0 {com.example.food001/com.example.food001.MainActivity}} token= android.os.BinderProxy@7cb25b0
2019-05-19 16:05:29.132 3402-3402/com.example.food001 D/ZrHung.AppEyeUiProbe: notify runnable to start.

Ответы [ 2 ]

2 голосов
/ 19 мая 2019

Попробуйте это:

  Log.i("UserResults - name", "Point 3");

    // List the products
    Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM product", null);
    int barcodeIndex = c.getColumnIndex("barcode");
    int descriptionIndex = c.getColumnIndex("description");
    int brandIndex = c.getColumnIndex("brand");

     if (c.moveToFirst()){
   do{
       String textString = "Product - [" + c.getString(barcodeIndex) + "]["+ c.getString(descriptionIndex)+ "][" + c.getString(brandIndex) + "]";
            Log.i("SQL-Log", textString );
   }while(c.moveToNext());
}
    c.close();
0 голосов
/ 19 мая 2019

Метод rawQuery() не вернет нулевой объект, будьте уверены в этом.
Таким образом, вам нужно только проверить, содержит ли курсор какие-либо строки, и поскольку позиция курсора в начале находится перед 1-й строкой (если есть строка), то вам нужно только проверить условие moveToNext().
Поэтому бросьте c.moveToFirst(); и сделайте следующее:

while (c.moveToNext()) {
    String textString = "Product - [" + c.getString(barcodeIndex) + "]["+ c.getString(descriptionIndex)+ "][" + c.getString(brandIndex) + "]";
    Log.i("SQL-Log", textString );
}
...