Last active
August 24, 2017 22:53
-
-
Save LuisHCK/3d760805dfd1e3bf6c05bc06afc9c324 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 prueba.com.prueba; | |
import android.app.TabActivity; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by luishck on 24/08/17. | |
*/ | |
public class ManejadorBaseDatos extends SQLiteOpenHelper { | |
// Declarar variables estaticas | |
private static final int VERSION_BASE_DATOS = 1; | |
private static final String NOMBRE_BASE_DATOS = "prueba"; | |
private static final String TABLA_CONTACTOS = "contactos"; | |
// Declarar columnas | |
private static final String ID = "id"; | |
private static final String NOMBRE = "nombre"; | |
private static final String CEDULA = "cedula"; | |
private static final String CORREO = "correo"; | |
// Asignar el contexto | |
public ManejadorBaseDatos(Context context) { | |
super(context, NOMBRE_BASE_DATOS, null, VERSION_BASE_DATOS); | |
} | |
// Crear tabla contacto | |
@Override | |
public void onCreate(SQLiteDatabase baseDatos) { | |
String crear_tabla = "CREATE TABLE " + TABLA_CONTACTOS + "(" + ID + | |
" INTEGER PRIMARY KEY AUTOINCREMENT, " + NOMBRE + " TEXT," + | |
CEDULA + " TEXT," + CORREO + " TEXT);"; | |
baseDatos.execSQL(crear_tabla); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase baseDatos, int vAnterior, int vNueva) { | |
baseDatos.execSQL("DROP TABLE IF EXISTS " + TABLA_CONTACTOS); | |
onCreate(baseDatos); | |
} | |
/** | |
* Funiones princales | |
* */ | |
void crearContacto(Contacto contacto) { | |
// Comprobar base de datos | |
SQLiteDatabase baseDatos = this.getWritableDatabase(); | |
// Crear un contenedor | |
ContentValues valores = new ContentValues(); | |
valores.put(NOMBRE, contacto.obtenerNombre()); | |
valores.put(CEDULA, contacto.obtenerCorreo()); | |
valores.put(CORREO, contacto.obtenerCorreo()); | |
// Insertar en la base de datos | |
baseDatos.insert(TABLA_CONTACTOS, null, valores); | |
} | |
// Obetener la lista de contactos | |
public List<Contacto> verContactos() { | |
List<Contacto> listaContactos = new ArrayList<Contacto>(); | |
// Crear un query | |
String queryObtener = "SELECT * FROM " + TABLA_CONTACTOS; | |
// Obtner la base datos | |
SQLiteDatabase baseDatos = this.getReadableDatabase(); | |
Cursor cursor = baseDatos.rawQuery(queryObtener, null); | |
// Leer las filas obtenidas y agreglarlas a la lista | |
if(cursor.moveToFirst()) { | |
do { | |
// Crear instancia de la clase contactos | |
Contacto contacto = new Contacto(); | |
contacto.asignarId(cursor.getInt(0)); | |
contacto.asignarNombre(cursor.getString(1)); | |
contacto.asignarCedula(cursor.getString(2)); | |
contacto.asignarCorreo(cursor.getString(3)); | |
// agregar el resultado a la lista | |
listaContactos.add(contacto); | |
} while (cursor.moveToNext()); | |
} | |
return listaContactos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment