Could not read row 6, col 6 from CursorWindow. (ANDROID)

0

Good morning

I'm trying to fill in a two-dimensional array, with data from my DB SQLite. But the following happens:

It's my first time with SQLite, by the way, and I was trying to find out if there was something like a "Result Set" or a "Data Table" ... and I found the so-called "Cursor". Ok, I started using it ... inserting a single row in each table (all normal) but now when I insert another row in my table matters, the application crashed when trying to navigate the cursor.

  

Caused by: java.lang.IllegalStateException: Could not read row 6, col 6   from CursorWindow. Make sure the Cursor is initialized correctly   before accessing data from it.

My table does not have 6 rows ... it only has 2. This is my code by the way:

 public String[][] getMaterias(){
    String[][] materias = new String[rows][cols];
    Cursor cursor = admin.selectLog(DBScheme.Tabla_Materias);
    cursor.moveToFirst();
    for(int f = 0; f < rows; f++){
        for(int c = 0; c < 6; c++){
            materias[f][c] = cursor.getString(cursor.getPosition());
            cursor.moveToNext();
        }
    }
    return materias;
}

My table is made up of 6 columns. I was observing how the cursor is handled and from what I could see:

  • cursor.getCount () and DatabaseUtils.queryNumEntries () only return the number of cells .

  • The methods moveToFirst (), moveToNext () ... are handled by the cells equally, but not by the rows.

(Correct me if my understanding is wrong ...)

Anyway, Cursor I associate it as well as a ArrayList ... so to speak. For the issue of taking out the same rows there are no problems, you simply divide the number of cells between the number of columns and that is it.

But what I have in doubt is how would you navigate in a table that contains more than one row? I thought that with that function it would be enough.

UPDATE

Experiment now with a while loop to see how it behaved and to my surprise, you are only capturing the values of the first row .

public String[][] getMaterias(){
    String[][] materias = new String[rows][6];
    Cursor cursor = admin.selectLog(DBScheme.Tabla_Materias);
    cursor.moveToFirst();
    try {
        while (cursor.moveToNext()) {
            Toast.makeText(getApplicationContext(),cursor.getString(cursor.getPosition()).toString(),Toast.LENGTH_SHORT).show();
        }
    }catch (Exception ex){
        ex.printStackTrace();
    }
    return materias;
}

How would you say to the cursor to continue with the next row? : S

    
asked by TwoDent 07.11.2017 в 00:10
source

1 answer

0

I feel like a fool right now, because the solution was in my eyes all this time ... hahaha! I just had to change this:

materias[f][c] = cursor.getString(cursor.getPosition());

for this:

materias[f][c] = cursor.getString(c);

Novice blunders, no doubt.

    
answered by 07.11.2017 в 16:58