Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DbHelper extends SQLiteOpenHelper {
- Toast toast = null;
- public static final String TAG = DbHelper.class.getSimpleName();
- public static final String DB_NAME = "myapp.db";
- public static final int DB_VERSION = 1;
- public static final String USER_TABLE = "users";
- public static final String COLUMN_ID = "_id";
- public static final String COLUMN_usurarios = "usurarios";
- public static final String COLUMN_ESTADO = "estado";
- public static final String COLUMN_PASS = "password";
- public static final String COLUMN_ADMINISTRADOR = "admin";
- /*
- create table users(
- id integer primary key autoincrement,
- usurarios text,
- password text);
- */
- public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
- + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
- + COLUMN_usurarios + " TEXT,"
- + COLUMN_ESTADO + " TEXT,"
- + COLUMN_PASS + " TEXT,"
- + COLUMN_ADMINISTRADOR + " boolean )";
- public DbHelper(Context applicationContext, Object o, SQLiteDatabase.CursorFactory factory, int i) {
- super(applicationContext, DB_NAME, factory, DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_TABLE_USERS);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
- onCreate(db);
- }
- private void verToast(String texto, Context context) { //muestro toast solo en el caso de que no esto otro abierto
- if (toast != null) {
- toast.cancel();
- }
- toast = Toast.makeText(context, texto, Toast.LENGTH_SHORT);
- toast.show(); //si hay otro abierto lo cancelo y muestro el siguiente toast
- }
- /**
- * Storing user details in database
- */
- public boolean btnReg (EditText nombre, EditText contraseña, Context context){
- SQLiteDatabase db = this.getReadableDatabase();
- String[] projection = {
- DbHelper.COLUMN_usurarios,
- DbHelper.COLUMN_PASS,
- DbHelper.COLUMN_ADMINISTRADOR
- };
- String selection = DbHelper.COLUMN_usurarios + " = ?";
- String[] selectionArgs = {nombre.toString()};
- Cursor c = db.query(
- DbHelper.USER_TABLE, // The table to query
- projection, // The columns to return
- selection, // The columns for the WHERE clause
- selectionArgs, // The values for the WHERE clause
- null, // don't group the rows
- null, // don't filter by row groups
- null // The sort order
- );
- c.moveToFirst();
- if (!(nombre.toString().isEmpty()) && !(contraseña.toString().isEmpty())){
- if (c.getCount() > 0){
- if (c.getString(c.getColumnIndexOrThrow(DbHelper.COLUMN_PASS)).equals(contraseña.toString())) {
- if (c.getInt(c.getColumnIndexOrThrow(DbHelper.COLUMN_ADMINISTRADOR)) == 1){
- return true;
- }else{
- verToast("No tienes permisos de administrador",context);
- }
- }else{
- verToast("Contraseña incorrecta",context);
- }
- }else{
- verToast("Ningun ususario con ese nombre registrado",context);
- }
- }else{
- verToast("Falta nombre o contraseña por introducir",context);
- }
- return false;
- }
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor mCursor = db.rawQuery("SELECT * FROM " + DbHelper.USER_TABLE + " WHERE " + DbHelper.COLUMN_ADMINISTRADOR+" =?", new String[] {"1"});
- if (!(mCursor != null && mCursor.getCount() > 0)){
- SQLiteDatabase dbr = this.getWritableDatabase();
- ContentValues cv = new ContentValues();
- cv.put(DbHelper.COLUMN_usurarios, "Admin");
- cv.put(DbHelper.COLUMN_PASS, 1234);
- cv.put(DbHelper.COLUMN_ADMINISTRADOR, 1);
- long newRowId = dbr.insert(DbHelper.USER_TABLE, null, cv);
- }
- public void addUser(String usurarios, String password, String estado,Context context) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_usurarios, usurarios);
- values.put(COLUMN_PASS, password);
- values.put(COLUMN_ESTADO, "Tu estado");
- long id = db.insert(USER_TABLE, null, values);
- db.close();
- Log.d(TAG, context.getString(R.string.UsuarioInsertado) + id);
- }
- public boolean getUser(String usurarios, String pass) {
- //HashMap<String, String> user = new HashMap<String, String>();
- String selectQuery = "select * from " + USER_TABLE + " where " +
- COLUMN_usurarios + " = " + "'" + usurarios + "'" + " and " + COLUMN_PASS + " = " + "'" + pass + "'";
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // Move to first row
- cursor.moveToFirst();
- if (cursor.getCount() > 0) {
- return true;
- }
- cursor.close();
- db.close();
- return false;
- }
- public String eliminar(String usuarios,Context context) {
- String mensaje = "";
- SQLiteDatabase db = this.getWritableDatabase();
- int cantidad = db.delete(USER_TABLE, COLUMN_usurarios + "=?", new String[]{usuarios});
- if (cantidad != 0) {
- mensaje = context.getString(R.string.EliminadoCorrectamente);
- } else {
- mensaje = context.getString(R.string.NoExisteEliminar);
- }
- db.close();
- return mensaje;
- }
- public ArrayList<usuarios> llenar_lv() {
- List<usuarios> usuarios = new ArrayList<>();
- SQLiteDatabase db = this.getWritableDatabase();
- String q = "SELECT * FROM users";
- Cursor registros = db.rawQuery(q, null);
- registros.moveToFirst();
- while (registros.moveToNext()) {
- String nombre = registros.getString(registros.getColumnIndex(DbHelper.COLUMN_usurarios));
- String estado = registros.getString(registros.getColumnIndex(DbHelper.COLUMN_ESTADO));
- usuarios user = new usuarios(nombre, estado);
- usuarios.add(user);
- }
- return (ArrayList<usuarios>) usuarios;
- }
- public String actualizar(String nombre, String contraseña,Context context) {
- String Mensaje = "";
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contenedor = new ContentValues();
- contenedor.put("password", contraseña);
- int cantidad = db.update(USER_TABLE, contenedor, COLUMN_usurarios + "='" + nombre + "'", null);
- if (cantidad != 0) {
- Mensaje = context.getString(R.string.ActualizadoCorrectamenteContraseña);
- } else {
- Mensaje = context.getString(R.string.NoActualziadoContraseña);
- }
- db.close();
- return Mensaje;
- }
- public String Estado(String nombre, String contraseña, String estado, Context context) {
- String Mensaje = "";
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contenedor = new ContentValues();
- contenedor.put("estado", estado);
- String[] projection = {DbHelper.COLUMN_usurarios, DbHelper.COLUMN_PASS,};
- String selection = DbHelper.COLUMN_usurarios + " = ?";
- String[] selectionArgs = {nombre.toString()};
- Cursor c = db.query(DbHelper.USER_TABLE, projection, selection, selectionArgs, null, null, null);
- c.moveToFirst();
- //int cantidad = db.update(USER_TABLE, contenedor, COLUMN_usurarios + "='" + nombre + "'", null);
- if (!(nombre.toString().isEmpty()) && !(contraseña.toString().isEmpty()) && !(estado.toString().isEmpty())) {
- if (c.getCount() > 0) {
- if (c.getString(c.getColumnIndexOrThrow(DbHelper.COLUMN_PASS)).equals(contraseña.toString())) {
- ContentValues cv = new ContentValues();
- cv.put(DbHelper.COLUMN_ESTADO, estado.toString());
- db.update(DbHelper.USER_TABLE, cv, DbHelper.COLUMN_usurarios + "=?", new String[]{nombre.toString()});
- Mensaje= context.getString(R.string.ActualizadoCorrectamente);
- //verToast (context.getString(R.string.ActualizadoCorrectamente), context);
- } else {
- Mensaje = context.getString(R.string.ContraseñaIncorrecta);
- //verToast(context.getString(R.string.ContraseñaIncorrecta), context);
- }
- } else {
- Mensaje = context.getString(R.string.NingunUsuarioConEseNombre);
- //verToast(context.getString(R.string.NingunUsuarioConEseNombre), context);
- }
- } else {
- Mensaje = context.getString(R.string.FaltaCmpoPorRellenar);
- //verToast(context.getString(R.string.FaltaCampoPorRellnar), context);
- }
- return Mensaje;
- }
Add Comment
Please, Sign In to add comment