· 6 years ago · Aug 14, 2019, 10:34 AM
1package com.repoai.myhome.Users;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.database.sqlite.SQLiteStatement;
9import android.util.Log;
10
11public class Users_SQLite extends SQLiteOpenHelper implements UserAble{
12 //name of the DB file in our phone
13 public static final String DB_NAME="smartHouse.db";
14 //name of the table
15 private final String TABLE_NAME="Users";
16 //an instance to our SQLiteDataBase
17 SQLiteDatabase db;
18
19 public Users_SQLite(Context context,String tableName){
20 super (context,DB_NAME,null,1);
21 //get the database for reading and writing by the super that we sent (db file will be smartHouse.db)
22 this.db = getWritableDatabase();
23 //you welcome
24 }
25
26 @Override
27 public void onCreate(SQLiteDatabase sqLiteDatabase) {
28 //SQL STRING
29 String sql = "CREATE TABLE IF NOT EXISTS "+this.TABLE_NAME+
30 " (id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, password TEXT, isEnabled INTEGER)";
31 Log.e("SQL", "onCreate: \n"+sql);
32 //create table if not exists
33 sqLiteDatabase.execSQL(sql);
34 }
35
36 @Override
37 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
38 //Do not touch this, unless you know what the fuck you are doing
39 //HE TPOH 3APA3A
40
41 }
42
43 //Diffrenet Method to enter a new user...
44 //METHOD I - short method , run the SQL command directly
45 public boolean addNewUser3(String userName, String password) {
46 //create sql string -> INSERT INTO [table name] (field1,field2) VALUES ('value1','value2')
47 String sql = "INSERT INTO "+this.TABLE_NAME+" (userName,password) VALUES ('"+userName+"','"+password+"')";
48 db.execSQL(sql);
49 return true;
50 }
51
52 //METHOD II - using SQL statment
53 public boolean addNewUser2(String userName, String password){
54 //create sql string -> INSERT INTO [table name] (field1,field2) VALUES ('value1','value2')
55 String sql = "INSERT INTO "+this.TABLE_NAME+" (userName,password) VALUES ('"+userName+"','"+password+"')";
56 //we are compiling the string sql into SQL Statment
57 SQLiteStatement statement = db.compileStatement(sql);
58 //we send the sql command, if we getting result of (-1) we got an error
59 long res=statement.executeInsert();
60 //return true if res != -1 (error)
61 return res!=(-1);
62 }
63
64 //METHOD III - recommended method
65 public boolean addNewUser(String userName, String password){
66 //create instance of ContentValues to hold our values
67 ContentValues myValues = new ContentValues();
68 //insert data by key and value
69 myValues.put("userName",userName);
70 myValues.put("password",password);
71 myValues.put("isEnabled",1);
72 //put our values into table and getting a result which reflects record id (-1 indicates an error)
73 long res=db.insert(this.TABLE_NAME,null,myValues);
74 //return true if res != -1 (error)
75 return res!=(-1);
76 }
77
78 @Override
79 public boolean userExists(String userName) {
80 Cursor res = db.rawQuery("SELECT * FROM "+this.TABLE_NAME+" WHERE userName='"+userName+"'",null);
81 //if the size of res (linked list) is 1 and above, the user exists :)
82 return res.getCount()>0;
83 }
84
85
86
87 @Override
88 public boolean checkUser(String userName, String password) {
89 String sql = "SELECT * FROM "+this.TABLE_NAME+" WHERE userName='"+userName+"' AND password='"+password+"'";
90 Cursor res = db.rawQuery(sql,null);
91 return res.getCount()>0;
92 }
93
94 @Override
95 public boolean deleteUser(String userName) {
96 String sql="DELETE FROM "+this.TABLE_NAME+" WHERE userName='"+userName+"'";
97 db.execSQL(sql);
98 return userExists(userName);
99 }
100
101 //method I with content values
102 @Override
103 public boolean changePassword(String userName, String oldPassword, String newPassword) {
104 if (checkUser(userName,oldPassword)){
105 ContentValues myValues = new ContentValues();
106 myValues.put("password",newPassword);
107 long res = db.update(this.TABLE_NAME,myValues,"userName='"+userName+"'",null);
108 return res!=(-1);
109 }
110 return false;
111 }
112
113 //method II with SQL statment
114 public boolean changePasswordSQL(String userName, String oldPassword, String newPassword){
115 if (checkUser(userName,oldPassword)) {
116 //UPDATE Users SET password='54321' WHERE userName='Roman'
117 String sql = "UPDATE "+this.TABLE_NAME+" SET password='" + newPassword + "' WHERE userName='" + userName + "'";
118 db.execSQL(sql);
119 return true;
120 }
121 return false;
122 }
123}