Issue
I'm working on a new app and I'm getting an error: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
I'm using an external database (assets folder) and trying to get the name column from Auditoria table, name column from Categoria table and pregunta column from Preguntas table. So, i made a join which works perfectly fine. I tested on DB Browser for SQLite.
Can someone help me?
public AuditoriaModel getCriterioById(int id) {
AuditoriaModel auditoriaModel = null;
openDatabase();
Cursor cursor = database.rawQuery("SELECT Auditoria.Name AS Auditorias, Categorias.Name AS Categoria, Preguntas.Pregunta AS Pregunta FROM Auditoria INNER JOIN Categorias ON Categorias.IdAuditoria = Auditoria.Id INNER JOIN Preguntas ON Preguntas.IdCategoria = Categorias.Id WHERE Preguntas.Id = ?", new String[]{String.valueOf(id)});
cursor.moveToFirst();
auditoriaModel = new AuditoriaModel (cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3), cursor.getString(4), cursor.getString(5));
//Obtendremos un solo resultado
cursor.close();
closeDatabase();
return auditoriaModel;
}
Solution
You're selecting three columns but trying to read six.
Either remove the extra get...()
reads, or add the missing columns to your SELECT
.
(Another problem: Check the result of moveToFirst()
in case the query matches nothing.)
Answered By - laalto
Answer Checked By - Marie Seifert (JavaFixing Admin)