Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- package com.androidtutorialshub.loginregister.sql;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import com.androidtutorialshub.loginregister.model.User;
- import java.util.ArrayList;
- import java.util.List;
- public class DatabaseHelper extends SQLiteOpenHelper {
- // Database Version
- private static final int DATABASE_VERSION = 1;
- // Database Name
- private static final String DATABASE_NAME = "UserManager.db";
- // User table name
- private static final String TABLE_USER = "user";
- // User Table Columns names
- private static final String COLUMN_USER_ID = "user_id";
- private static final String COLUMN_USER_NAME = "user_name";
- private static final String COLUMN_USER_EMAIL = "user_email";
- private static final String COLUMN_USER_PASSWORD = "user_password";
- // create table sql query
- private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
- + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + " TEXT,"
- + COLUMN_USER_EMAIL + " TEXT," + COLUMN_USER_PASSWORD + " TEXT" + ")";
- // drop table sql query
- private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;
- /**
- * Constructor
- *
- * @param context
- */
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_USER_TABLE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- //Drop User Table if exist
- db.execSQL(DROP_USER_TABLE);
- // Create tables again
- onCreate(db);
- }
- /**
- * This method is to create user record
- *
- * @param user
- */
- public void addUser(User user) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_USER_NAME, user.getName());
- values.put(COLUMN_USER_EMAIL, user.getEmail());
- values.put(COLUMN_USER_PASSWORD, user.getPassword());
- // Inserting Row
- db.insert(TABLE_USER, null, values);
- db.close();
- }
- /**
- * This method is to fetch all user and return the list of user records
- *
- * @return list
- */
- public List<User> getAllUser() {
- // array of columns to fetch
- String[] columns = {
- COLUMN_USER_ID,
- COLUMN_USER_EMAIL,
- COLUMN_USER_NAME,
- COLUMN_USER_PASSWORD
- };
- // sorting orders
- String sortOrder =
- COLUMN_USER_NAME + " ASC";
- List<User> userList = new ArrayList<User>();
- SQLiteDatabase db = this.getReadableDatabase();
- // query the user table
- /**
- * Here query function is used to fetch records from user table this function works like we use sql query.
- * SQL query equivalent to this query function is
- * SELECT user_id,user_name,user_email,user_password FROM user ORDER BY user_name;
- */
- Cursor cursor = db.query(TABLE_USER, //Table to query
- columns, //columns to return
- null, //columns for the WHERE clause
- null, //The values for the WHERE clause
- null, //group the rows
- null, //filter by row groups
- sortOrder); //The sort order
- // Traversing through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- User user = new User();
- user.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_USER_ID))));
- user.setName(cursor.getString(cursor.getColumnIndex(COLUMN_USER_NAME)));
- user.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_USER_EMAIL)));
- user.setPassword(cursor.getString(cursor.getColumnIndex(COLUMN_USER_PASSWORD)));
- // Adding user record to list
- userList.add(user);
- } while (cursor.moveToNext());
- }
- cursor.close();
- db.close();
- // return user list
- return userList;
- }
- /**
- * This method to update user record
- *
- * @param user
- */
- public void updateUser(User user) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_USER_NAME, user.getName());
- values.put(COLUMN_USER_EMAIL, user.getEmail());
- values.put(COLUMN_USER_PASSWORD, user.getPassword());
- // updating row
- db.update(TABLE_USER, values, COLUMN_USER_ID + " = ?",
- new String[]{String.valueOf(user.getId())});
- db.close();
- }
- /**
- * This method is to delete user record
- *
- * @param user
- */
- public void deleteUser(User user) {
- SQLiteDatabase db = this.getWritableDatabase();
- // delete user record by id
- db.delete(TABLE_USER, COLUMN_USER_ID + " = ?",
- new String[]{String.valueOf(user.getId())});
- db.close();
- }
- /**
- * This method to check user exist or not
- *
- * @param email
- * @return true/false
- */
- public boolean checkUser(String email) {
- // array of columns to fetch
- String[] columns = {
- COLUMN_USER_ID
- };
- SQLiteDatabase db = this.getReadableDatabase();
- // selection criteria
- String selection = COLUMN_USER_EMAIL + " = ?";
- // selection argument
- String[] selectionArgs = {email};
- // query user table with condition
- /**
- * Here query function is used to fetch records from user table this function works like we use sql query.
- * SQL query equivalent to this query function is
- * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com';
- */
- Cursor cursor = db.query(TABLE_USER, //Table to query
- columns, //columns to return
- selection, //columns for the WHERE clause
- selectionArgs, //The values for the WHERE clause
- null, //group the rows
- null, //filter by row groups
- null); //The sort order
- int cursorCount = cursor.getCount();
- cursor.close();
- db.close();
- if (cursorCount > 0) {
- return true;
- }
- return false;
- }
- /**
- * This method to check user exist or not
- *
- * @param email
- * @param password
- * @return true/false
- */
- public boolean checkUser(String email, String password) {
- // array of columns to fetch
- String[] columns = {
- COLUMN_USER_ID
- };
- SQLiteDatabase db = this.getReadableDatabase();
- // selection criteria
- String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
- // selection arguments
- String[] selectionArgs = {email, password};
- // query user table with conditions
- /**
- * Here query function is used to fetch records from user table this function works like we use sql query.
- * SQL query equivalent to this query function is
- * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
- */
- Cursor cursor = db.query(TABLE_USER, //Table to query
- columns, //columns to return
- selection, //columns for the WHERE clause
- selectionArgs, //The values for the WHERE clause
- null, //group the rows
- null, //filter by row groups
- null); //The sort order
- int cursorCount = cursor.getCount();
- cursor.close();
- db.close();
- if (cursorCount > 0) {
- return true;
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment