Display Sqlite table field values in the Console

Asked

Viewed 67 times

0

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

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 get no return when including the lines:

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

and

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

The table is populated, the simple fact of removing

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

causes the output of other fields to appear normally. And I still don’t understand where my mistake is.

Thanks in advance for your help !!

1 answer

2


The column id is the type INTEGER.

Instead of

cursor.getString(indiceColunaId)

use

cursor.getInt(indiceColunaId)

Like Log.i() wait a String has to convert before using:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.