-
-
Save parzibyte/860964f4d659044424343e7a54b893ea 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 in.parzibyte.crudsqlite.controllers; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import in.parzibyte.crudsqlite.AyudanteBaseDeDatos; | |
import in.parzibyte.crudsqlite.modelos.Mascota; | |
import java.util.ArrayList; | |
public class MascotasController | |
{ | |
private String NOMBRE_TABLA = "mascotas"; | |
private AyudanteBaseDeDatos ayudanteBaseDeDatos; | |
public MascotasController(Context paramContext) | |
{ | |
ayudanteBaseDeDatos = new AyudanteBaseDeDatos(paramContext); | |
} | |
public int eliminarMascota(Mascota paramMascota) | |
{ | |
SQLiteDatabase localSQLiteDatabase = ayudanteBaseDeDatos.getWritableDatabase(); | |
long l = paramMascota.getId(); | |
return localSQLiteDatabase.delete(NOMBRE_TABLA, "id = ?", new String[] { String.valueOf(l) }); | |
} | |
public int guardarCambios(Mascota paramMascota) | |
{ | |
SQLiteDatabase localSQLiteDatabase = ayudanteBaseDeDatos.getWritableDatabase(); | |
ContentValues localContentValues = new ContentValues(); | |
localContentValues.put("nombre", paramMascota.getNombre()); | |
localContentValues.put("edad", Integer.valueOf(paramMascota.getEdad())); | |
long l = paramMascota.getId(); | |
return localSQLiteDatabase.update(NOMBRE_TABLA, localContentValues, "id = ?", new String[] { String.valueOf(l) }); | |
} | |
public long nuevaMascota(Mascota paramMascota) | |
{ | |
SQLiteDatabase localSQLiteDatabase = ayudanteBaseDeDatos.getWritableDatabase(); | |
ContentValues localContentValues = new ContentValues(); | |
localContentValues.put("nombre", paramMascota.getNombre()); | |
localContentValues.put("edad", Integer.valueOf(paramMascota.getEdad())); | |
return localSQLiteDatabase.insert(NOMBRE_TABLA, null, localContentValues); | |
} | |
public ArrayList obtenerMascotas() | |
{ | |
ArrayList localArrayList = new ArrayList(); | |
Cursor localCursor = ayudanteBaseDeDatos.getReadableDatabase().query(NOMBRE_TABLA, new String[] { "nombre", "edad", "id" }, null, null, null, null, null); | |
if (localCursor == null) { | |
return localArrayList; | |
} | |
if (!localCursor.moveToFirst()) { | |
return localArrayList; | |
} | |
do | |
{ | |
localArrayList.add(new Mascota(localCursor.getString(0), localCursor.getInt(1), localCursor.getLong(2))); | |
} while (localCursor.moveToNext()); | |
localCursor.close(); | |
return localArrayList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment