· 6 years ago · Jan 29, 2020, 01:12 PM
1package com.joshua.r0th.crud2;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.content.Intent;
6import android.database.Cursor;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.os.Bundle;
10import android.os.StatFs;
11import android.view.View;
12import android.widget.EditText;
13import android.widget.TextView;
14
15public class SqliteHelper extends SQLiteOpenHelper {
16
17
18 //DATABASE NAME
19 public static final String DATABASE_NAME = "loopwiki.com";
20
21 //DATABASE VERSION
22 public static final int DATABASE_VERSION = 1;
23 public static String data1="";
24
25 public static final String TABLE_USERS = "users";
26 public static final String DATAPASS = data1 ;
27
28 //TABLE USERS COLUMNS
29 //ID COLUMN @primaryKey
30 public static final String KEY_ID = "id";
31
32 //COLUMN user name
33 public static final String KEY_USER_NAME = "username";
34
35 //COLUMN email
36 public static final String KEY_EMAIL = "email";
37
38 //COLUMN password
39 public static final String KEY_PASSWORD = "password";
40
41 //COLUMN No telpon
42 public static final String KEY_TELP = "notelp";
43
44 //COLUMN RT
45 public static final String KEY_RT = "rt";
46
47 //COLUMN RW
48 public static final String KEY_RW = "rw";
49
50 //COLUMN NO Rumah
51 public static final String KEY_NORMH = "normh";
52
53 //SQL for creating users table
54 public static final String SQL_TABLE_USERS = " CREATE TABLE " + TABLE_USERS
55 + " ( "
56 + KEY_ID + " INTEGER PRIMARY KEY, "
57 + KEY_USER_NAME + " TEXT, "
58 + KEY_EMAIL + " TEXT, "
59 + KEY_PASSWORD + " TEXT"
60 + KEY_TELP + " TEXT"
61 + KEY_RT + " TEXT"
62 + KEY_RW + " TEXT"
63 + KEY_NORMH + " TEXT"
64 + " ) ";
65
66
67 public SqliteHelper(Context context) {
68 super(context, DATABASE_NAME, null, DATABASE_VERSION);
69 }
70
71 @Override
72 public void onCreate(SQLiteDatabase sqLiteDatabase) {
73 //Create Table when oncreate gets called
74 sqLiteDatabase.execSQL(SQL_TABLE_USERS);
75
76 }
77
78 @Override
79 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
80 //drop table to create new one if database version updated
81 sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_USERS);
82 }
83
84 //using this method we can add users to user table
85 public void addUser(User user) {
86
87 //get writable database
88 SQLiteDatabase db = this.getWritableDatabase();
89
90 //create content values to insert
91 ContentValues values = new ContentValues();
92
93 //Put username in @values
94 values.put(KEY_USER_NAME, user.userName);
95
96 //Put email in @values
97 values.put(KEY_EMAIL, user.email);
98
99 //Put password in @values
100 values.put(KEY_PASSWORD, user.password);
101
102
103 // insert row
104 long todo_id = db.insert(TABLE_USERS, null, values);
105 }
106
107 public User Authenticate(User user) {
108 SQLiteDatabase db = this.getReadableDatabase();
109 Cursor cursor = db.query(TABLE_USERS,// Selecting Table
110 new String[]{KEY_ID, KEY_USER_NAME, KEY_EMAIL, KEY_PASSWORD},//Selecting columns want to query
111 KEY_EMAIL + "=?",
112 new String[]{user.email},//Where clause
113 null, null, null);
114
115 if (cursor != null && cursor.moveToFirst()&& cursor.getCount()>0) {
116 //if cursor has value then in user database there is user associated with this given email
117 User user1 = new User(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3));
118
119 //Match both passwords check they are same or not
120 if (user.password.equalsIgnoreCase(user1.password)) {
121 return user1;
122 }
123 }
124
125 //if user password does not matches or there is no record with that email then return @false
126 return null;
127 }
128
129 public boolean isEmailExists(String email) {
130 SQLiteDatabase db = this.getReadableDatabase();
131 Cursor cursor = db.query(TABLE_USERS,// Selecting Table
132 new String[]{KEY_ID, KEY_USER_NAME, KEY_EMAIL, KEY_PASSWORD},//Selecting columns want to query
133 KEY_EMAIL + "=?",
134 new String[]{email},//Where clause
135 null, null, null);
136
137 if (cursor != null && cursor.moveToFirst()&& cursor.getCount()>0) {
138 //if cursor has value then in user database there is user associated with this given email so return true
139 return true;
140 }
141
142 //if email does not exist return false
143 return false;
144 }
145 public String getData() {
146 Cursor cursor = null;
147
148 StringBuilder empName = new StringBuilder();
149 try {
150 SQLiteDatabase db = this.getWritableDatabase();
151 cursor = db.rawQuery("SELECT * FROM users ", null);
152 if (cursor.getCount() > 0) {
153 while (cursor.moveToNext()) {
154
155 empName.append(cursor.getString(cursor.getColumnIndex("username"))+" ");
156
157
158 }
159 }
160 return empName.toString();
161 } catch (Exception e) {
162 e.printStackTrace();
163 } finally {
164 if (cursor != null) cursor.close();
165 }
166
167 return null;
168 }
169
170
171 public String getData1() {
172
173 Cursor cursor = null;
174
175
176
177 StringBuilder empName = new StringBuilder();
178 try {
179 String move;
180 Bundle b = new Bundle();
181 move = b.getString("UserInput");
182
183 SQLiteDatabase db = this.getWritableDatabase();
184
185 cursor = db.rawQuery("SELECT username FROM " + TABLE_USERS +" WHERE email =" + move.trim(), null);
186 if (cursor.getCount() > 0) {
187 while (cursor.moveToNext()) {
188
189 empName.append(cursor.getString(cursor.getColumnIndex("email"))+" ");
190
191
192 }
193 }
194 return empName.toString();
195 } catch (Exception e) {
196 e.printStackTrace();
197 } finally {
198 if (cursor != null) cursor.close();
199 }
200
201 return null;
202 }
203 public String getData2() {
204 Cursor cursor = null;
205
206 StringBuilder empName = new StringBuilder();
207 try {
208 SQLiteDatabase db = this.getWritableDatabase();
209 cursor = db.rawQuery("SELECT * FROM users ", null);
210 if (cursor.getCount() > 0) {
211 while (cursor.moveToNext()) {
212
213 empName.append(cursor.getString(cursor.getColumnIndex("password"))+" ");
214
215
216 }
217 }
218 return empName.toString();
219 } catch (Exception e) {
220 e.printStackTrace();
221 } finally {
222 if (cursor != null) cursor.close();
223 }
224
225 return null;
226 }
227}