Guest User

Untitled

a guest
Nov 4th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public static final String USER_TABLE = "users";
  2. public static final String COLUMN_ID = "_id";
  3. public static final String COLUMN_EMAIL = "email";
  4. public static final String COLUMN_PASS = "password";
  5.  
  6. public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
  7. + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  8. + COLUMN_EMAIL + " TEXT,"
  9. + COLUMN_PASS + " TEXT);";
  10.  
  11. public DbHelper(Context context) {
  12. super(context, DB_NAME, null, DB_VERSION);
  13. }
  14.  
  15. @Override
  16. public void onCreate(SQLiteDatabase db) {
  17. db.execSQL(CREATE_TABLE_USERS);
  18. }
  19.  
  20. @Override
  21. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  22. db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
  23. onCreate(db);
  24. }
  25.  
  26. /**
  27. * Storing user details in database
  28. * */
  29. public void addUser(String email, String password) {
  30. SQLiteDatabase db = this.getWritableDatabase();
  31.  
  32. ContentValues values = new ContentValues();
  33. values.put(COLUMN_EMAIL, email);
  34. values.put(COLUMN_PASS, password);
  35.  
  36. long id = db.insert(USER_TABLE, null, values);
  37. db.close();
  38.  
  39. Log.d(TAG, "user inserted" + id);
  40. }
  41.  
  42. public boolean getUser(String email, String pass){
  43. //HashMap<String, String> user = new HashMap<String, String>();
  44. String selectQuery = "select * from " + USER_TABLE + " where " +
  45. COLUMN_EMAIL + " = " + "'"+email+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'";
  46.  
  47. SQLiteDatabase db = this.getReadableDatabase();
  48. Cursor cursor = db.rawQuery(selectQuery, null);
  49. // Move to first row
  50. cursor.moveToFirst();
  51. if (cursor.getCount() > 0) {
  52.  
  53. return true;
  54. }
  55. cursor.close();
  56. db.close();
  57.  
  58. return false;
  59. }
Add Comment
Please, Sign In to add comment