Advertisement
Guest User

Untitled

a guest
Jan 8th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 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. import android.widget.TextView;
  10.  
  11.  
  12. public class DbHelper extends SQLiteOpenHelper {
  13. public static final String TAG = DbHelper.class.getSimpleName();
  14. public static final String DB_NAME = "carsharing.db";
  15. public static final int DB_VERSION = 1;
  16.  
  17. public static final String USER_TABLE = "users";
  18. public static final String COLUMN_ID = "_id";
  19. public static final String COLUMN_EMAIL = "email";
  20. public static final String COLUMN_PASS = "password";
  21.  
  22. public static final String RES_TABLE="reservation";
  23. public static final String COLUMN_NAME="name";
  24. public static final String COLUMN_SURNAME="surname";
  25. public static final String COLUMN_ADDRESS="address";
  26. public static final String COLUMN_PHONE="phone";
  27. public static final String COLUMN_START="start";
  28. public static final String COLUMN_DESTINATION="destination";
  29.  
  30.  
  31. /*
  32. create table users(
  33. id integer primary key autoincrement,
  34. email text,
  35. password text);
  36. */
  37. public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
  38. + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  39. + COLUMN_EMAIL + " TEXT,"
  40. + COLUMN_PASS + " TEXT);";
  41.  
  42. public static final String CREATE_TABLE_RESERVATION = "CREATE TABLE " + RES_TABLE + "("
  43. + COLUMN_NAME + " TEXT,"
  44. + COLUMN_SURNAME + " TEXT,"
  45. + COLUMN_ADDRESS + " TEXT,"
  46. + COLUMN_PHONE + " TEXT,"
  47. + COLUMN_START + " TEXT,"
  48. + COLUMN_DESTINATION + " TEXT);";
  49.  
  50.  
  51. public DbHelper(Context context) {
  52. super(context, DB_NAME, null, DB_VERSION);
  53.  
  54. }
  55.  
  56. @Override
  57. public void onCreate(SQLiteDatabase db) {
  58. db.execSQL(CREATE_TABLE_USERS);
  59. db.execSQL(CREATE_TABLE_RESERVATION);
  60. }
  61.  
  62. @Override
  63. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  64. db.execSQL("DROP TABLE IF EXIST" + USER_TABLE);
  65. onCreate(db);
  66.  
  67. db.execSQL("DROP TABLE IF EXIST" + RES_TABLE);
  68. onCreate(db);
  69. }
  70.  
  71.  
  72. public void addUser(String email, String password) {
  73. SQLiteDatabase db = this.getWritableDatabase();
  74.  
  75. ContentValues values = new ContentValues();
  76. values.put(COLUMN_EMAIL, email);
  77. values.put(COLUMN_PASS, password);
  78.  
  79. long id = db.insert(USER_TABLE, null, values);
  80. db.close();
  81.  
  82. Log.d(TAG, "user inserted" + id);
  83. }
  84.  
  85. public boolean getUser(String email, String pass) {
  86. //HashMap<String, String> user = new HashMap<String, String>();
  87. String selectQuery = "select * from " + USER_TABLE + " where " +
  88. COLUMN_EMAIL + " = " + "'" + email + "'" + " and " + COLUMN_PASS + " = " + "'" + pass + "'";
  89.  
  90. SQLiteDatabase db = this.getReadableDatabase();
  91. Cursor cursor = db.rawQuery(selectQuery, null);
  92. // Move to first row
  93. cursor.moveToFirst();
  94. if (cursor.getCount() > 0) {
  95.  
  96. return true;
  97. }
  98. cursor.close();
  99. db.close();
  100.  
  101. return false;
  102. }
  103.  
  104.  
  105. // method for inserting data from method reservation
  106. public void addReservation(String name, String surname, String address, String phone, String start, String destination) {
  107. SQLiteDatabase db = this.getWritableDatabase();
  108. ContentValues values = new ContentValues();
  109. values.put(COLUMN_NAME, name);
  110. values.put(COLUMN_SURNAME, surname);
  111. values.put(COLUMN_ADDRESS, address);
  112. values.put(COLUMN_PHONE, phone);
  113. values.put(COLUMN_START, start);
  114. values.put(COLUMN_DESTINATION, destination);
  115.  
  116. long a = db.insert(RES_TABLE, null, values);
  117. db.close();
  118.  
  119. Log.e(TAG, "Data insetred" + a);
  120. }
  121.  
  122.  
  123. //Get data from Reservation
  124. public boolean getData(String name, String surname, String address, String phone, String start, String destination) {
  125. String query = "select * from " + RES_TABLE + " where " +
  126. COLUMN_NAME + " = " + "'" + name + "'" + " , " + COLUMN_SURNAME + " = " + "'" + surname + "'" +
  127. COLUMN_ADDRESS + " = " + "'" + address + "'" + " , " + COLUMN_PHONE + " = " + "'" + phone + "'" +
  128. COLUMN_START + " = " + "'" + start + "'" + " , " + COLUMN_DESTINATION + " = " + "'" + destination + "'";
  129.  
  130. SQLiteDatabase db = this.getReadableDatabase();
  131. Cursor cursor = db.rawQuery(query, null);
  132. // Move to first row
  133. cursor.moveToFirst();
  134. if (cursor.getCount() > 0) {
  135.  
  136. return true;
  137. }
  138. cursor.close();
  139. db.close();
  140.  
  141. return false;
  142. }
  143.  
  144. //insert data iinto Reservation
  145. public boolean insertReservation(String name, String surname, String address, String phone, String start, String destination) {
  146. SQLiteDatabase db = this.getWritableDatabase();
  147. ContentValues contentValues = new ContentValues();
  148. contentValues.put("name",name);
  149. contentValues.put("surname",surname);
  150. contentValues.put("address",address);
  151. contentValues.put("phone",phone);
  152. contentValues.put("start",start);
  153. contentValues.put("destination",destination);
  154. db.insert("reservation",null,contentValues);
  155. return true;
  156.  
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement