Guest User

Untitled

a guest
Jan 6th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. package com.example.demir.carsharing;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.util.Log;
  9.  
  10.  
  11. public class DbHelper extends SQLiteOpenHelper {
  12. public static final String TAG = DbHelper.class.getSimpleName();
  13. public static final String DB_NAME="carsharing.db";
  14. public static final int DB_VERSION =1;
  15.  
  16. public static final String USER_TABLE = "users";
  17. public static final String COLUMN_ID = "_id";
  18. public static final String COLUMN_EMAIL = "email";
  19. public static final String COLUMN_PASS = "password";
  20.  
  21. /*
  22. create table users(
  23. id integer primary key autoincrement,
  24. email text,
  25. password text);
  26. */
  27. public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
  28. + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  29. + COLUMN_EMAIL + " TEXT,"
  30. + COLUMN_PASS + " TEXT);";
  31.  
  32. public DbHelper(Context context) {
  33. super(context, DB_NAME, null, DB_VERSION);
  34. }
  35.  
  36. @Override
  37. public void onCreate(SQLiteDatabase db) {
  38. db.execSQL(CREATE_TABLE_USERS);
  39. }
  40.  
  41. @Override
  42. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  43. db.execSQL("DROP TABLE IF EXIST"+USER_TABLE);
  44. onCreate(db);
  45. }
  46.  
  47. public void addUser(String email, String password) {
  48. SQLiteDatabase db = this.getWritableDatabase();
  49.  
  50. ContentValues values = new ContentValues();
  51. values.put(COLUMN_EMAIL, email);
  52. values.put(COLUMN_PASS, password);
  53.  
  54. long id = db.insert(USER_TABLE, null, values);
  55. db.close();
  56.  
  57. Log.d(TAG, "user inserted" + id);
  58. }
  59.  
  60. public boolean getUser(String email, String pass){
  61. //HashMap<String, String> user = new HashMap<String, String>();
  62. String selectQuery = "select * from " + USER_TABLE + " where " +
  63. COLUMN_EMAIL + " = " + "'"+email+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'";
  64.  
  65. SQLiteDatabase db = this.getReadableDatabase();
  66. Cursor cursor = db.rawQuery(selectQuery, null);
  67. // Move to first row
  68. cursor.moveToFirst();
  69. if (cursor.getCount() > 0) {
  70.  
  71. return true;
  72. }
  73. cursor.close();
  74. db.close();
  75.  
  76. return false;
  77. }
  78. }
Add Comment
Please, Sign In to add comment