Guest User

Untitled

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