Advertisement
don_dema

Untitled

Jan 6th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 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. * public static final String TABLE_NAME="reservation";
  23. public static final String COL_1="id";
  24. public static final String COL_2="name";
  25. public static final String COL_3="address";
  26. public static final String COL_4="phone_number";
  27. public static final String COL_5="starting_point";
  28. public static final String COL_6="destination";
  29.  
  30. *
  31. * */
  32.  
  33.  
  34.  
  35.  
  36.  
  37. /*
  38. create table users(
  39. id integer primary key autoincrement,
  40. email text,
  41. password text);
  42. */
  43. public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
  44. + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  45. + COLUMN_EMAIL + " TEXT,"
  46. + COLUMN_PASS + " TEXT);";
  47.  
  48.  
  49.  
  50. public DbHelper(Context context) {
  51. super(context, DB_NAME, null, DB_VERSION);
  52. }
  53.  
  54.  
  55.  
  56. @Override
  57. public void onCreate(SQLiteDatabase db) {
  58. db.execSQL(CREATE_TABLE_USERS);
  59. }
  60.  
  61. @Override
  62. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  63. db.execSQL("DROP TABLE IF EXIST"+USER_TABLE);
  64. onCreate(db);
  65. }
  66.  
  67. public void addUser(String email, String password) {
  68. SQLiteDatabase db = this.getWritableDatabase();
  69.  
  70. ContentValues values = new ContentValues();
  71. values.put(COLUMN_EMAIL, email);
  72. values.put(COLUMN_PASS, password);
  73.  
  74. long id = db.insert(USER_TABLE, null, values);
  75. db.close();
  76.  
  77. Log.d(TAG, "user inserted" + id);
  78. }
  79.  
  80. public boolean getUser(String email, String pass){
  81. //HashMap<String, String> user = new HashMap<String, String>();
  82. String selectQuery = "select * from " + USER_TABLE + " where " +
  83. COLUMN_EMAIL + " = " + "'"+email+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'";
  84.  
  85. SQLiteDatabase db = this.getReadableDatabase();
  86. Cursor cursor = db.rawQuery(selectQuery, null);
  87. // Move to first row
  88. cursor.moveToFirst();
  89. if (cursor.getCount() > 0) {
  90.  
  91. return true;
  92. }
  93. cursor.close();
  94. db.close();
  95.  
  96. return false;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement