-
-
Save parzibyte/70e9719b97cbca28f122d8a4fe3b1129 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
package me.parzibyte.crudsqlite.controllers; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import java.util.ArrayList; | |
import me.parzibyte.crudsqlite.AyudanteBaseDeDatos; | |
import me.parzibyte.crudsqlite.modelos.Mascota; | |
public class MascotasController { | |
private AyudanteBaseDeDatos ayudanteBaseDeDatos; | |
private String NOMBRE_TABLA = "mascotas"; | |
public MascotasController(Context contexto) { | |
ayudanteBaseDeDatos = new AyudanteBaseDeDatos(contexto); | |
} | |
public int eliminarMascota(Mascota mascota) { | |
SQLiteDatabase baseDeDatos = ayudanteBaseDeDatos.getWritableDatabase(); | |
String[] argumentos = {String.valueOf(mascota.getId())}; | |
return baseDeDatos.delete(NOMBRE_TABLA, "id = ?", argumentos); | |
} | |
public long nuevaMascota(Mascota mascota) { | |
// writable porque vamos a insertar | |
SQLiteDatabase baseDeDatos = ayudanteBaseDeDatos.getWritableDatabase(); | |
ContentValues valoresParaInsertar = new ContentValues(); | |
valoresParaInsertar.put("nombre", mascota.getNombre()); | |
valoresParaInsertar.put("edad", mascota.getEdad()); | |
return baseDeDatos.insert(NOMBRE_TABLA, null, valoresParaInsertar); | |
} | |
public int guardarCambios(Mascota mascotaEditada) { | |
SQLiteDatabase baseDeDatos = ayudanteBaseDeDatos.getWritableDatabase(); | |
ContentValues valoresParaActualizar = new ContentValues(); | |
valoresParaActualizar.put("nombre", mascotaEditada.getNombre()); | |
valoresParaActualizar.put("edad", mascotaEditada.getEdad()); | |
// where id... | |
String campoParaActualizar = "id = ?"; | |
// ... = idMascota | |
String[] argumentosParaActualizar = {String.valueOf(mascotaEditada.getId())}; | |
return baseDeDatos.update(NOMBRE_TABLA, valoresParaActualizar, campoParaActualizar, argumentosParaActualizar); | |
} | |
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