Implementación de métodos.
Agregar
public void registrar(View view){
adminDB admin = new adminDB(this, "BaseDatos", null, 1);
SQLiteDatabase BaseDatos = admin.getWritableDatabase();
String cedula= etCedula.getText().toString();
String nombre= etNombre.getText().toString();
String telefono= etTelefono.getText().toString();
if(!cedula.isEmpty() && !nombre.isEmpty() && !telefono.isEmpty()){
ContentValues registro= new ContentValues();
registro.put("cedula", cedula);
registro.put("nombre", nombre);
registro.put("telefono", telefono);
BaseDatos.insert("usuarios", null,registro);
BaseDatos.close();
etCedula.setText("");
etNombre.setText("");
etTelefono.setText("");
Toast.makeText(this, "Registro almacenado exitosamente", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Debes ingresar todos los campos", Toast.LENGTH_LONG).show();
}
}
Buscar
public void consultar(View view){
adminDB admin = new adminDB(this, "BaseDatos", null, 1);
SQLiteDatabase BaseDatos = admin.getWritableDatabase();
String cedula = etCedula.getText().toString();
if(!cedula.isEmpty()){
Cursor fila=BaseDatos.rawQuery("select nombre, telefono from usuarios where cedula="+cedula,null);
if(fila.moveToFirst()){
etNombre.setText(fila.getString(0));
etTelefono.setText(fila.getString(1));
BaseDatos.close();
}else {
Toast.makeText(this, "No se encontro el usuario", Toast.LENGTH_LONG).show();
}
}
}
Actualizar
public void actualizar(View view) {
adminDB admin = new adminDB(this, "BaseDatos", null, 1);
SQLiteDatabase BaseDatos = admin.getWritableDatabase();
String cedula = etCedula.getText().toString();
String nombre = etNombre.getText().toString();
String telefono = etTelefono.getText().toString();
if (!cedula.isEmpty() && !nombre.isEmpty() && !telefono.isEmpty()) {
ContentValues valores = new ContentValues();
valores.put("nombre", nombre);
valores.put("telefono", telefono);
int rowsUpdated = BaseDatos.update("usuarios", valores, "cedula=?", new String[]{cedula});
BaseDatos.close();
if (rowsUpdated > 0) {
etCedula.setText("");
etNombre.setText("");
etTelefono.setText("");
Toast.makeText(this, "Registro actualizado exitosamente", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No se encontró el usuario con la cédula especificada", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Debes ingresar todos los campos para actualizar el registro", Toast.LENGTH_LONG).show();
}
}
Eliminar
public void eliminar(View view) {
adminDB admin = new adminDB(this, "BaseDatos", null, 1);
SQLiteDatabase BaseDatos = admin.getWritableDatabase();
String cedula = etCedula.getText().toString();
if (!cedula.isEmpty()) {
int rowsDeleted = BaseDatos.delete("usuarios", "cedula=?", new String[]{cedula});
BaseDatos.close();
if (rowsDeleted > 0) {
etCedula.setText("");
etNombre.setText("");
etTelefono.setText("");
Toast.makeText(this, "Registro eliminado exitosamente", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No se encontró el usuario con la cédula especificada", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Debes ingresar la cédula para eliminar el registro", Toast.LENGTH_LONG).show();
}
}
Comentarios
Publicar un comentario