Show in Log the data with which you are filling a cursor in android

1

I have a cursor that I'm filling from the SQLite database, but apparently it's empty, I want to show it in the console using a log.

cursor=db.obtenerSaldoClienteFavor(idClienteA);

This is my cursor, thank you very much

    
asked by David Gutierrez 03.01.2019 в 21:44
source

2 answers

2
  • First you have to call cursor.moveToFirst() .
  • Then Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cursor))
answered by 03.01.2019 / 23:40
source
1

To do this you need to know the names of the fields and the data type of the fields that your query gets, since based on this you will determine which method you will use to obtain the value, for example:

  

getString (int columnIndex) Returns the value of the requested column   like a chain.

     

getInt (int columnIndex) Returns the value of the requested column   as an int.

     

getLong (int columnIndex) Returns the value of the requested column   like a long one.

     

getDouble (int columnIndex) Returns the value of the requested column   as double.

     

getBlob (int columnIndex) Returns the value of the requested column   as a byte array.

and using a loop using while , you can get the values using the getColumnIndex () which is where you would specify the name of the field.

Assuming that your query gets the fields idCliente and nombre , this would be an example:

Cursor cursor  = db.obtenerSaldoClienteFavor(idClienteA);
while (cursor.moveToNext()) {
    Log.i("Dato cursor", "idCliente " + cursor.getInt(cursor.getColumnIndex("idCliente")) + " nombre: " + cursor.getString(cursor.getColumnIndex("nombre")));
}
c.close();

* It is very important to close the cursor using the close() method when you finish getting the data.

Get the number of elements in the cursor

To get the number of elements contained in your cursor you can use the method getCount ()

Example:

Cursor cursor  = db.obtenerSaldoClienteFavor(idClienteA);
Log.i("Datos cursor", "Número de elementos: " + cursor.getCount(); 
...
...
    
answered by 04.01.2019 в 18:45