jose
jose

Reputation: 1

How to use a Cursor?

I have an app that receives the id of an animal and pass that id to a method from the database that will be read several times in a do-while. I want to know why this error happens?

This is my code:

bd = new BaseDados(getApplicationContext());
    Cursor cc = bd.getIdAnimal(chipnumber);
    if (cc.moveToFirst()) {

    idanimal = cc.getInt(cc.getColumnIndex("idanimal"));
}

Cursor ccc = bd.getGruupsnosAnimals(idanimal);

do{
    if(ccc.moveToFirst()){

        String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
    }

}while (ccc.moveToNext());

This is my error:

   Process: com.example.nobre.myapplication, PID: 4529
   android.database.sqlite.SQLiteException: not an error (code 0)
   at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
   at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845)
   at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
   at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
   at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
   at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
   at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
   at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
   at com.example.nobre.myapplication.Activities.VerAnimaisActivity.showDialog(VerAnimaisActivity.java:193)
   at com.example.nobre.myapplication.Activities.VerAnimaisActivity$2.onClick(VerAnimaisActivity.java:153)
   at android.view.View.performClick(View.java:4780)
   at android.view.View$PerformClick.run(View.java:19866)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5254)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Upvotes: 0

Views: 436

Answers (3)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21766

Try this:

// Move the cursor to the first row  if cursor is not empty
if(ccc.moveToFirst()) {

    do{
          String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
    }while (ccc.moveToNext()); // Move cursor to next row until it pass last entry
}

Instead of:

do{
       if(ccc.moveToFirst()) {
          String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
       }
}while (ccc.moveToNext());

Upvotes: -1

Mikhail Krutov
Mikhail Krutov

Reputation: 702

do{

    if(ccc.moveToFirst()){

        String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
    }


}while (ccc.moveToNext());

this is an infinite loop, while you have next item in your cursor, you move to first one, and next iteration you would again move to first one.

Also do not forget to call Cursor.close() after usage.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93728

Exception: not an error? That's a new one.

Your bottom loop is a bit weird and may be a bug- you're moving to next but may not even have moved to first if that failed. And if it succeeded you'll infinitely loop. Try this instead:

if(ccc.moveToFirst()){

    do{

        String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
    }while (ccc.moveToNext());


}

Upvotes: 1

Related Questions