Guest User

Untitled

a guest
May 3rd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1.  
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 11
  13. 12
  14. 13
  15. 14
  16. 15
  17. 16
  18. 17
  19. 18
  20. 19
  21. 20
  22. 21
  23. 22
  24. 23
  25. 24
  26. 25
  27. 26
  28. 27
  29. 28
  30. 29
  31. 30
  32. 31
  33. 32
  34. 33
  35. 34
  36. 35
  37. 36
  38. 37
  39. 38
  40. 39
  41. 40
  42. 41
  43. 42
  44. 43
  45. 44
  46. 45
  47. 46
  48. 47
  49. 48
  50. 49
  51. 50
  52. 51
  53. 52
  54. 53
  55. 54
  56. 55
  57. 56
  58. 57
  59. 58
  60. 59
  61. 60
  62. 61
  63. 62
  64. 63
  65. 64
  66. 65
  67. 66
  68. 67
  69. 68
  70. 69
  71. 70
  72. 71
  73. 72
  74. 73
  75. 74
  76. 75
  77. 76
  78. 77
  79. 78
  80. 79
  81. 80
  82. 81
  83. 82
  84. 83
  85. 84
  86. 85
  87. 86
  88. 87
  89. 88
  90. 89
  91. 90
  92. 91
  93. 92
  94. 93
  95. 94
  96. 95
  97. 96
  98. 97
  99. 98
  100. 99
  101. 100
  102. 101
  103. 102
  104. 103
  105. 104
  106. 105
  107. 106
  108. 107
  109. 108
  110. 109
  111. 110
  112. 111
  113. 112
  114. 113
  115. 114
  116. 115
  117. 116
  118. 117
  119. 118
  120. 119
  121. 120
  122. 121
  123. 122
  124. 123
  125. 124
  126. 125
  127. 126
  128. 127
  129. 128
  130. 129
  131. 130
  132. 131
  133. 132
  134. 133
  135. 134
  136. 135
  137. 136
  138. 137
  139. 138
  140. 139
  141. 140
  142. 141
  143. 142
  144. 143
  145. 144
  146. 145
  147. 146
  148. 147
  149. 148
  150. 149
  151. 150
  152. 151
  153. 152
  154. 153
  155. 154
  156. 155
  157. 156
  158. 157
  159. 158
  160. 159
  161. 160
  162. 161
  163. 162
  164. 163
  165. 164
  166. 165
  167. 166
  168. 167
  169. 168
  170. 169
  171. 170
  172. 171
  173. 172
  174. 173
  175. 174
  176. 175
  177. 176
  178. 177
  179. 178
  180. 179
  181. 180
  182. 181
  183. 182
  184. 183
  185. 184
  186. 185
  187. 186
  188. 187
  189. 188
  190. 189
  191. 190
  192. 191
  193. 192
  194. 193
  195. 194
  196. 195
  197. 196
  198. 197
  199. 198
  200. 199
  201. 200
  202. 201
  203. 202
  204. 203
  205. 204
  206. 205
  207. 206
  208. 207
  209. 208
  210. 209
  211. 210
  212. 211
  213. 212
  214. 213
  215. 214
  216. 215
  217. 216
  218. 217
  219. 218
  220. 219
  221. 220
  222. 221
  223. 222
  224. 223
  225. 224
  226. 225
  227. 226
  228. 227
  229. 228
  230. 229
  231. 230
  232. 231
  233. 232
  234. 233
  235. 234
  236. 235
  237. 236
  238. 237
  239. 238
  240. 239
  241. 240
  242. 241
  243. 242
  244. 243
  245. 244
  246. 245
  247. 246
  248. 247
  249. 248
  250. 249
  251. 250
  252. 251
  253. 252
  254. 253
  255. 254
  256. 255
  257. 256
  258. 257
  259. package com.androidtutorialshub.loginregister.sql;
  260.  
  261. import android.content.ContentValues;
  262. import android.content.Context;
  263. import android.database.Cursor;
  264. import android.database.sqlite.SQLiteDatabase;
  265. import android.database.sqlite.SQLiteOpenHelper;
  266.  
  267. import com.androidtutorialshub.loginregister.model.User;
  268.  
  269. import java.util.ArrayList;
  270. import java.util.List;
  271.  
  272. public class DatabaseHelper extends SQLiteOpenHelper {
  273.  
  274. // Database Version
  275. private static final int DATABASE_VERSION = 1;
  276.  
  277. // Database Name
  278. private static final String DATABASE_NAME = "UserManager.db";
  279.  
  280. // User table name
  281. private static final String TABLE_USER = "user";
  282.  
  283. // User Table Columns names
  284. private static final String COLUMN_USER_ID = "user_id";
  285. private static final String COLUMN_USER_NAME = "user_name";
  286. private static final String COLUMN_USER_EMAIL = "user_email";
  287. private static final String COLUMN_USER_PASSWORD = "user_password";
  288.  
  289. // create table sql query
  290. private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
  291. + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + " TEXT,"
  292. + COLUMN_USER_EMAIL + " TEXT," + COLUMN_USER_PASSWORD + " TEXT" + ")";
  293.  
  294. // drop table sql query
  295. private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;
  296.  
  297. /**
  298. * Constructor
  299. *
  300. * @param context
  301. */
  302. public DatabaseHelper(Context context) {
  303. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  304. }
  305.  
  306. @Override
  307. public void onCreate(SQLiteDatabase db) {
  308. db.execSQL(CREATE_USER_TABLE);
  309. }
  310.  
  311.  
  312. @Override
  313. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  314.  
  315. //Drop User Table if exist
  316. db.execSQL(DROP_USER_TABLE);
  317.  
  318. // Create tables again
  319. onCreate(db);
  320.  
  321. }
  322.  
  323. /**
  324. * This method is to create user record
  325. *
  326. * @param user
  327. */
  328. public void addUser(User user) {
  329. SQLiteDatabase db = this.getWritableDatabase();
  330.  
  331. ContentValues values = new ContentValues();
  332. values.put(COLUMN_USER_NAME, user.getName());
  333. values.put(COLUMN_USER_EMAIL, user.getEmail());
  334. values.put(COLUMN_USER_PASSWORD, user.getPassword());
  335.  
  336. // Inserting Row
  337. db.insert(TABLE_USER, null, values);
  338. db.close();
  339. }
  340.  
  341. /**
  342. * This method is to fetch all user and return the list of user records
  343. *
  344. * @return list
  345. */
  346. public List<User> getAllUser() {
  347. // array of columns to fetch
  348. String[] columns = {
  349. COLUMN_USER_ID,
  350. COLUMN_USER_EMAIL,
  351. COLUMN_USER_NAME,
  352. COLUMN_USER_PASSWORD
  353. };
  354. // sorting orders
  355. String sortOrder =
  356. COLUMN_USER_NAME + " ASC";
  357. List<User> userList = new ArrayList<User>();
  358.  
  359. SQLiteDatabase db = this.getReadableDatabase();
  360.  
  361. // query the user table
  362. /**
  363. * Here query function is used to fetch records from user table this function works like we use sql query.
  364. * SQL query equivalent to this query function is
  365. * SELECT user_id,user_name,user_email,user_password FROM user ORDER BY user_name;
  366. */
  367. Cursor cursor = db.query(TABLE_USER, //Table to query
  368. columns, //columns to return
  369. null, //columns for the WHERE clause
  370. null, //The values for the WHERE clause
  371. null, //group the rows
  372. null, //filter by row groups
  373. sortOrder); //The sort order
  374.  
  375.  
  376. // Traversing through all rows and adding to list
  377. if (cursor.moveToFirst()) {
  378. do {
  379. User user = new User();
  380. user.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_USER_ID))));
  381. user.setName(cursor.getString(cursor.getColumnIndex(COLUMN_USER_NAME)));
  382. user.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_USER_EMAIL)));
  383. user.setPassword(cursor.getString(cursor.getColumnIndex(COLUMN_USER_PASSWORD)));
  384. // Adding user record to list
  385. userList.add(user);
  386. } while (cursor.moveToNext());
  387. }
  388. cursor.close();
  389. db.close();
  390.  
  391. // return user list
  392. return userList;
  393. }
  394.  
  395. /**
  396. * This method to update user record
  397. *
  398. * @param user
  399. */
  400. public void updateUser(User user) {
  401. SQLiteDatabase db = this.getWritableDatabase();
  402.  
  403. ContentValues values = new ContentValues();
  404. values.put(COLUMN_USER_NAME, user.getName());
  405. values.put(COLUMN_USER_EMAIL, user.getEmail());
  406. values.put(COLUMN_USER_PASSWORD, user.getPassword());
  407.  
  408. // updating row
  409. db.update(TABLE_USER, values, COLUMN_USER_ID + " = ?",
  410. new String[]{String.valueOf(user.getId())});
  411. db.close();
  412. }
  413.  
  414. /**
  415. * This method is to delete user record
  416. *
  417. * @param user
  418. */
  419. public void deleteUser(User user) {
  420. SQLiteDatabase db = this.getWritableDatabase();
  421. // delete user record by id
  422. db.delete(TABLE_USER, COLUMN_USER_ID + " = ?",
  423. new String[]{String.valueOf(user.getId())});
  424. db.close();
  425. }
  426.  
  427. /**
  428. * This method to check user exist or not
  429. *
  430. * @param email
  431. * @return true/false
  432. */
  433. public boolean checkUser(String email) {
  434.  
  435. // array of columns to fetch
  436. String[] columns = {
  437. COLUMN_USER_ID
  438. };
  439. SQLiteDatabase db = this.getReadableDatabase();
  440.  
  441. // selection criteria
  442. String selection = COLUMN_USER_EMAIL + " = ?";
  443.  
  444. // selection argument
  445. String[] selectionArgs = {email};
  446.  
  447. // query user table with condition
  448. /**
  449. * Here query function is used to fetch records from user table this function works like we use sql query.
  450. * SQL query equivalent to this query function is
  451. * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com';
  452. */
  453. Cursor cursor = db.query(TABLE_USER, //Table to query
  454. columns, //columns to return
  455. selection, //columns for the WHERE clause
  456. selectionArgs, //The values for the WHERE clause
  457. null, //group the rows
  458. null, //filter by row groups
  459. null); //The sort order
  460. int cursorCount = cursor.getCount();
  461. cursor.close();
  462. db.close();
  463.  
  464. if (cursorCount > 0) {
  465. return true;
  466. }
  467.  
  468. return false;
  469. }
  470.  
  471. /**
  472. * This method to check user exist or not
  473. *
  474. * @param email
  475. * @param password
  476. * @return true/false
  477. */
  478. public boolean checkUser(String email, String password) {
  479.  
  480. // array of columns to fetch
  481. String[] columns = {
  482. COLUMN_USER_ID
  483. };
  484. SQLiteDatabase db = this.getReadableDatabase();
  485. // selection criteria
  486. String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
  487.  
  488. // selection arguments
  489. String[] selectionArgs = {email, password};
  490.  
  491. // query user table with conditions
  492. /**
  493. * Here query function is used to fetch records from user table this function works like we use sql query.
  494. * SQL query equivalent to this query function is
  495. * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
  496. */
  497. Cursor cursor = db.query(TABLE_USER, //Table to query
  498. columns, //columns to return
  499. selection, //columns for the WHERE clause
  500. selectionArgs, //The values for the WHERE clause
  501. null, //group the rows
  502. null, //filter by row groups
  503. null); //The sort order
  504.  
  505. int cursorCount = cursor.getCount();
  506.  
  507. cursor.close();
  508. db.close();
  509. if (cursorCount > 0) {
  510. return true;
  511. }
  512.  
  513. return false;
  514. }
  515. }
Add Comment
Please, Sign In to add comment