Created
February 4, 2019 16:28
-
-
Save parzibyte/abbd3e1dd6cfa308ace0c96b13583cd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ArrayList<Mascota> obtenerMascotas() { | |
ArrayList<Mascota> mascotas = new ArrayList<>(); | |
// readable porque no vamos a modificar, solamente leer | |
SQLiteDatabase baseDeDatos = ayudanteBaseDeDatos.getReadableDatabase(); | |
// SELECT nombre, edad, id | |
String[] columnasAConsultar = {"nombre", "edad", "id"}; | |
Cursor cursor = baseDeDatos.query( | |
NOMBRE_TABLA,//from mascotas | |
columnasAConsultar, | |
null, | |
null, | |
null, | |
null, | |
null | |
); | |
if (cursor == null) { | |
/* | |
Salimos aquí porque hubo un error, regresar | |
lista vacía | |
*/ | |
return mascotas; | |
} | |
// Si no hay datos, igualmente regresamos la lista vacía | |
if (!cursor.moveToFirst()) return mascotas; | |
// En caso de que sí haya, iteramos y vamos agregando los | |
// datos a la lista de mascotas | |
do { | |
// El 0 es el número de la columna, como seleccionamos | |
// nombre, edad,id entonces el nombre es 0, edad 1 e id es 2 | |
String nombreObtenidoDeBD = cursor.getString(0); | |
int edadObtenidaDeBD = cursor.getInt(1); | |
long idMascota = cursor.getLong(2); | |
Mascota mascotaObtenidaDeBD = new Mascota(nombreObtenidoDeBD, edadObtenidaDeBD, idMascota); | |
mascotas.add(mascotaObtenidaDeBD); | |
} while (cursor.moveToNext()); | |
// Fin del ciclo. Cerramos cursor y regresamos la lista de mascotas :) | |
cursor.close(); | |
return mascotas; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment