Display values of SQLite table fields in the Console

0

I'm using the following code snippet to return the values of the fields in a DB in the Android Studio console:

db.execSQL("CREATE TABLE IF NOT EXISTS pessoas(id INTEGER PRIMARY KEY AUTOINCREMENT, nome VARCHAR, idade INT(3))");    

Cursor cursor = db.rawQuery("SELECT * FROM pessoas", null);

//Recuperando os índices das colunas
int indiceColunaId = cursor.getColumnIndex("id");
int indiceColunaNome = cursor.getColumnIndex("nome");
int indiceColunaIdade = cursor.getColumnIndex("idade");

// Movendo o cursor para o primeiro item
cursor.moveToFirst();

while (cursor != null)
{

     // Recuperando os valores armazenados no cursor
     Log.i("Resultado - id: ", cursor.getString(indiceColunaId));
     Log.i("Resultado - nome: ", cursor.getString(indiceColunaNome));
     Log.i("Resultado - idade: ", cursor.getString(indiceColunaIdade));


     // movendo o cursor para o próximo item
     cursor.moveToNext();
}

Unfortunately without success, I do not get a return by adding the lines:

int indiceColunaId = cursor.getColumnIndex("id");

and

Log.i("Resultado - id: ", cursor.getString(indiceColunaId));

The table is populated, simply removing

  

Log.i ("Result - id:", cursor.getString (IndiceColumnId));

causes the output of the other fields to appear normally. And I still do not understand where my error is.

Thanks in advance for the help !!

    
asked by anonymous 11.12.2016 / 18:40

1 answer

2

The id column is of type INTEGER .

Instead of

cursor.getString(indiceColunaId)

use

cursor.getInt(indiceColunaId)

As Log.i() expects a String must convert before using:

Log.i("Resultado - id: ", String.valueOf(cursor.getInt(indiceColunaId)));
    
11.12.2016 / 22:12