· 7 years ago · Jan 28, 2019, 04:10 PM
1package com.banyucenter.atlasapp;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9
10
11public class SqliteHelper extends SQLiteOpenHelper {
12
13 //DATABASE NAME
14 public static final String DATABASE_NAME = "atlasgalon";
15
16 //DATABASE VERSION
17 public static final int DATABASE_VERSION = 1;
18
19 //TABLE NAME
20 public static final String TABLE_USERS = "users";
21
22 //TABLE USERS COLUMNS
23 //ID COLUMN @primaryKey
24 public static final String KEY_ID = "id";
25
26 //COLUMN user name
27 public static final String KEY_USER_NAME = "username";
28
29 //COLUMN email
30 public static final String KEY_EMAIL = "email";
31
32 //COLUMN password
33 public static final String KEY_PASSWORD = "password";
34
35 //SQL for creating users table
36 public static final String SQL_TABLE_USERS = " CREATE TABLE " + TABLE_USERS
37 + " ( "
38 + KEY_ID + " INTEGER PRIMARY KEY, "
39 + KEY_USER_NAME + " TEXT, "
40 + KEY_EMAIL + " TEXT, "
41 + KEY_PASSWORD + " TEXT"
42 + " ) ";
43
44
45 public SqliteHelper(Context context) {
46 super(context, DATABASE_NAME, null, DATABASE_VERSION);
47 }
48
49 @Override
50 public void onCreate(SQLiteDatabase sqLiteDatabase) {
51 //Create Table when oncreate gets called
52 sqLiteDatabase.execSQL(SQL_TABLE_USERS);
53
54 }
55
56 @Override
57 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
58 //drop table to create new one if database version updated
59 sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_USERS);
60 }
61
62 //using this method we can add users to user table
63 public void addUser(User user) {
64
65 //get writable database
66 SQLiteDatabase db = this.getWritableDatabase();
67
68 //create content values to insert
69 ContentValues values = new ContentValues();
70
71 //Put username in @values
72 values.put(KEY_USER_NAME, user.userName);
73
74 //Put email in @values
75 values.put(KEY_EMAIL, user.email);
76
77 //Put password in @values
78 values.put(KEY_PASSWORD, user.password);
79
80 // insert row
81 long todo_id = db.insert(TABLE_USERS, null, values);
82 }
83
84 public User Authenticate(User user) {
85 SQLiteDatabase db = this.getReadableDatabase();
86 Cursor cursor = db.query(TABLE_USERS,// Selecting Table
87 new String[]{KEY_ID, KEY_USER_NAME, KEY_EMAIL, KEY_PASSWORD},//Selecting columns want to query
88 KEY_EMAIL + "=?",
89 new String[]{user.email},//Where clause
90 null, null, null);
91
92 if (cursor != null && cursor.moveToFirst()&& cursor.getCount()>0) {
93 //if cursor has value then in user database there is user associated with this given email
94 User user1 = new User(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3));
95
96 //Match both passwords check they are same or not
97 if (user.password.equalsIgnoreCase(user1.password)) {
98 return user1;
99 }
100 }
101
102 //if user password does not matches or there is no record with that email then return @false
103 return null;
104 }
105
106 public boolean isEmailExists(String email) {
107 SQLiteDatabase db = this.getReadableDatabase();
108 Cursor cursor = db.query(TABLE_USERS,// Selecting Table
109 new String[]{KEY_ID, KEY_USER_NAME, KEY_EMAIL, KEY_PASSWORD},//Selecting columns want to query
110 KEY_EMAIL + "=?",
111 new String[]{email},//Where clause
112 null, null, null);
113
114 if (cursor != null && cursor.moveToFirst()&& cursor.getCount()>0) {
115 //if cursor has value then in user database there is user associated with this given email so return true
116 return true;
117 }
118
119 //if email does not exist return false
120 return false;
121 }
122}