Guest User

Untitled

a guest
Dec 9th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. public class DbHelper extends SQLiteOpenHelper {
  2. Toast toast = null;
  3. public static final String TAG = DbHelper.class.getSimpleName();
  4. public static final String DB_NAME = "myapp.db";
  5. public static final int DB_VERSION = 1;
  6.  
  7. public static final String USER_TABLE = "users";
  8. public static final String COLUMN_ID = "_id";
  9. public static final String COLUMN_usurarios = "usurarios";
  10. public static final String COLUMN_ESTADO = "estado";
  11. public static final String COLUMN_PASS = "password";
  12. public static final String COLUMN_ADMINISTRADOR = "admin";
  13.  
  14.  
  15. /*
  16. create table users(
  17. id integer primary key autoincrement,
  18. usurarios text,
  19. password text);
  20. */
  21. public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
  22. + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  23. + COLUMN_usurarios + " TEXT,"
  24. + COLUMN_ESTADO + " TEXT,"
  25. + COLUMN_PASS + " TEXT,"
  26. + COLUMN_ADMINISTRADOR + " boolean )";
  27.  
  28. public DbHelper(Context applicationContext, Object o, SQLiteDatabase.CursorFactory factory, int i) {
  29. super(applicationContext, DB_NAME, factory, DB_VERSION);
  30. }
  31.  
  32. @Override
  33. public void onCreate(SQLiteDatabase db) {
  34. db.execSQL(CREATE_TABLE_USERS);
  35. }
  36.  
  37.  
  38. @Override
  39. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  40. db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
  41.  
  42. onCreate(db);
  43. }
  44.  
  45. private void verToast(String texto, Context context) { //muestro toast solo en el caso de que no esto otro abierto
  46. if (toast != null) {
  47. toast.cancel();
  48. }
  49. toast = Toast.makeText(context, texto, Toast.LENGTH_SHORT);
  50. toast.show(); //si hay otro abierto lo cancelo y muestro el siguiente toast
  51. }
  52.  
  53. /**
  54. * Storing user details in database
  55. */
  56. public boolean btnReg (EditText nombre, EditText contraseña, Context context){
  57.  
  58. SQLiteDatabase db = this.getReadableDatabase();
  59.  
  60. String[] projection = {
  61. DbHelper.COLUMN_usurarios,
  62. DbHelper.COLUMN_PASS,
  63. DbHelper.COLUMN_ADMINISTRADOR
  64. };
  65.  
  66. String selection = DbHelper.COLUMN_usurarios + " = ?";
  67. String[] selectionArgs = {nombre.toString()};
  68.  
  69. Cursor c = db.query(
  70. DbHelper.USER_TABLE, // The table to query
  71. projection, // The columns to return
  72. selection, // The columns for the WHERE clause
  73. selectionArgs, // The values for the WHERE clause
  74. null, // don't group the rows
  75. null, // don't filter by row groups
  76. null // The sort order
  77. );
  78.  
  79. c.moveToFirst();
  80. if (!(nombre.toString().isEmpty()) && !(contraseña.toString().isEmpty())){
  81. if (c.getCount() > 0){
  82. if (c.getString(c.getColumnIndexOrThrow(DbHelper.COLUMN_PASS)).equals(contraseña.toString())) {
  83. if (c.getInt(c.getColumnIndexOrThrow(DbHelper.COLUMN_ADMINISTRADOR)) == 1){
  84. return true;
  85. }else{
  86. verToast("No tienes permisos de administrador",context);
  87. }
  88. }else{
  89. verToast("Contraseña incorrecta",context);
  90. }
  91. }else{
  92. verToast("Ningun ususario con ese nombre registrado",context);
  93. }
  94. }else{
  95. verToast("Falta nombre o contraseña por introducir",context);
  96. }
  97. return false;
  98. }
  99.  
  100. SQLiteDatabase db = this.getReadableDatabase();
  101. Cursor mCursor = db.rawQuery("SELECT * FROM " + DbHelper.USER_TABLE + " WHERE " + DbHelper.COLUMN_ADMINISTRADOR+" =?", new String[] {"1"});
  102.  
  103. if (!(mCursor != null && mCursor.getCount() > 0)){
  104. SQLiteDatabase dbr = this.getWritableDatabase();
  105. ContentValues cv = new ContentValues();
  106. cv.put(DbHelper.COLUMN_usurarios, "Admin");
  107. cv.put(DbHelper.COLUMN_PASS, 1234);
  108. cv.put(DbHelper.COLUMN_ADMINISTRADOR, 1);
  109.  
  110. long newRowId = dbr.insert(DbHelper.USER_TABLE, null, cv);
  111. }
  112.  
  113. public void addUser(String usurarios, String password, String estado,Context context) {
  114.  
  115. SQLiteDatabase db = this.getWritableDatabase();
  116.  
  117. ContentValues values = new ContentValues();
  118. values.put(COLUMN_usurarios, usurarios);
  119. values.put(COLUMN_PASS, password);
  120. values.put(COLUMN_ESTADO, "Tu estado");
  121.  
  122. long id = db.insert(USER_TABLE, null, values);
  123. db.close();
  124.  
  125. Log.d(TAG, context.getString(R.string.UsuarioInsertado) + id);
  126. }
  127.  
  128.  
  129. public boolean getUser(String usurarios, String pass) {
  130. //HashMap<String, String> user = new HashMap<String, String>();
  131. String selectQuery = "select * from " + USER_TABLE + " where " +
  132. COLUMN_usurarios + " = " + "'" + usurarios + "'" + " and " + COLUMN_PASS + " = " + "'" + pass + "'";
  133.  
  134. SQLiteDatabase db = this.getReadableDatabase();
  135. Cursor cursor = db.rawQuery(selectQuery, null);
  136. // Move to first row
  137. cursor.moveToFirst();
  138. if (cursor.getCount() > 0) {
  139.  
  140. return true;
  141. }
  142. cursor.close();
  143. db.close();
  144.  
  145. return false;
  146. }
  147.  
  148. public String eliminar(String usuarios,Context context) {
  149. String mensaje = "";
  150.  
  151. SQLiteDatabase db = this.getWritableDatabase();
  152.  
  153. int cantidad = db.delete(USER_TABLE, COLUMN_usurarios + "=?", new String[]{usuarios});
  154.  
  155. if (cantidad != 0) {
  156. mensaje = context.getString(R.string.EliminadoCorrectamente);
  157. } else {
  158. mensaje = context.getString(R.string.NoExisteEliminar);
  159. }
  160. db.close();
  161. return mensaje;
  162. }
  163.  
  164.  
  165. public ArrayList<usuarios> llenar_lv() {
  166. List<usuarios> usuarios = new ArrayList<>();
  167. SQLiteDatabase db = this.getWritableDatabase();
  168. String q = "SELECT * FROM users";
  169. Cursor registros = db.rawQuery(q, null);
  170. registros.moveToFirst();
  171.  
  172.  
  173. while (registros.moveToNext()) {
  174. String nombre = registros.getString(registros.getColumnIndex(DbHelper.COLUMN_usurarios));
  175. String estado = registros.getString(registros.getColumnIndex(DbHelper.COLUMN_ESTADO));
  176.  
  177. usuarios user = new usuarios(nombre, estado);
  178. usuarios.add(user);
  179.  
  180.  
  181. }
  182. return (ArrayList<usuarios>) usuarios;
  183. }
  184.  
  185. public String actualizar(String nombre, String contraseña,Context context) {
  186. String Mensaje = "";
  187. SQLiteDatabase db = this.getWritableDatabase();
  188. ContentValues contenedor = new ContentValues();
  189. contenedor.put("password", contraseña);
  190. int cantidad = db.update(USER_TABLE, contenedor, COLUMN_usurarios + "='" + nombre + "'", null);
  191. if (cantidad != 0) {
  192.  
  193. Mensaje = context.getString(R.string.ActualizadoCorrectamenteContraseña);
  194. } else {
  195. Mensaje = context.getString(R.string.NoActualziadoContraseña);
  196. }
  197. db.close();
  198. return Mensaje;
  199. }
  200.  
  201. public String Estado(String nombre, String contraseña, String estado, Context context) {
  202. String Mensaje = "";
  203. SQLiteDatabase db = this.getWritableDatabase();
  204. ContentValues contenedor = new ContentValues();
  205. contenedor.put("estado", estado);
  206. String[] projection = {DbHelper.COLUMN_usurarios, DbHelper.COLUMN_PASS,};
  207. String selection = DbHelper.COLUMN_usurarios + " = ?";
  208. String[] selectionArgs = {nombre.toString()};
  209. Cursor c = db.query(DbHelper.USER_TABLE, projection, selection, selectionArgs, null, null, null);
  210. c.moveToFirst();
  211. //int cantidad = db.update(USER_TABLE, contenedor, COLUMN_usurarios + "='" + nombre + "'", null);
  212. if (!(nombre.toString().isEmpty()) && !(contraseña.toString().isEmpty()) && !(estado.toString().isEmpty())) {
  213. if (c.getCount() > 0) {
  214. if (c.getString(c.getColumnIndexOrThrow(DbHelper.COLUMN_PASS)).equals(contraseña.toString())) {
  215. ContentValues cv = new ContentValues();
  216. cv.put(DbHelper.COLUMN_ESTADO, estado.toString());
  217.  
  218. db.update(DbHelper.USER_TABLE, cv, DbHelper.COLUMN_usurarios + "=?", new String[]{nombre.toString()});
  219. Mensaje= context.getString(R.string.ActualizadoCorrectamente);
  220. //verToast (context.getString(R.string.ActualizadoCorrectamente), context);
  221.  
  222. } else {
  223. Mensaje = context.getString(R.string.ContraseñaIncorrecta);
  224. //verToast(context.getString(R.string.ContraseñaIncorrecta), context);
  225. }
  226. } else {
  227. Mensaje = context.getString(R.string.NingunUsuarioConEseNombre);
  228. //verToast(context.getString(R.string.NingunUsuarioConEseNombre), context);
  229. }
  230. } else {
  231. Mensaje = context.getString(R.string.FaltaCmpoPorRellenar);
  232. //verToast(context.getString(R.string.FaltaCampoPorRellnar), context);
  233. }
  234. return Mensaje;
  235. }
Add Comment
Please, Sign In to add comment