Llamada:
AdminSQLiteOpenHelper
Una vez creada le añadiremos la palabra reservada “extends” y luego SQLiteOpenHelper cómo se muestra a continuación:
public class AdminSQLiteOpenHelper extends SQLiteOpenHelper{
La clase SQLiteOpenHelper nos pedirá
Implementar 2 métodos llamados OnCreate y OnUpdate
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
Y por último nos pedirá un constructor para
Los 2 métodos anteriores
El cual es:
public AdminSQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
Creamos la tabla usuario dentro de
OnCreate
Mediante la siguiente instrucción:
db.execSQL("create table cliente(id int primary key,nombre text,numero integer)");
public void alta(View v) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
String id = et1.getText().toString();
String name = et2.getText().toString();
String phone = et3.getText().toString();
ContentValues registro = new ContentValues();
registro.put(“id", id);
registro.put(“nombre", name);
registro.put(“numero", phone);
bd.insert(“cliente", null, registro);
bd.close();
et1.setText("");
et2.setText("");
et3.setText("");
Toast.makeText(this, "Se cargaron guardaron los datos del cliente",
Toast.LENGTH_SHORT).show();
}
public void consultacodigo(View v) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
String id = et1.getText().toString();
Cursor fila = bd.rawQuery(
"select nombre,numero from cliente where id=" + id, null);
if (fila.moveToFirst()) {
et2.setText(fila.getString(0));
et3.setText(fila.getString(1));
}
else{
Toast.makeText(this, "No existe un cliente con dicho código",
Toast.LENGTH_SHORT).show();
bd.close();
}
}
public void baja(View v) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
String id= et1.getText().toString();
int cant = bd.delete(“cliente", “id=" + id, null);
bd.close();
et1.setText("");
et2.setText("");
et3.setText("");
if (cant == 1){
Toast.makeText(this, "Se borró el cliente con dicho código",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "No existe un cliente con dicho código",
Toast.LENGTH_SHORT).show();
}
}
public void modi(View v) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
String id = et1.getText().toString();
String name = et2.getText().toString();
String phone = et3.getText().toString();
ContentValues registro = new ContentValues();
registro.put(“id", id);
registro.put(“nombre", name);
registro.put(“numero", phone);
int cant = bd.update(“cliente", registro, “id=" + id, null);
bd.close();
if (cant == 1) {
Toast.makeText(this, "se modificaron los datos del cliente", Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(this, "no existe un artículo con el código ingresado",
Toast.LENGTH_SHORT).show();
}
}