Guest User

Untitled

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