· 6 years ago · Aug 18, 2019, 08:46 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;
9
10public class Users_SQLite extends SQLiteOpenHelper implements UserAble {
11 //name of the DB file in our phone
12 public static final String DB_NAME = "SmartHouse.db";
13 //name of the table
14 private final String TABLE_NAME = "users";
15 //an instance SQLite
16 SQLiteDatabase db;
17
18 public Users_SQLite(Context context, String tableName) {
19 super(context, DB_NAME, null, 1);
20 //get the database for reading and writing by the super that we sent
21 this.db = getWritableDatabase();
22 }
23
24 @Override
25 public void onCreate(SQLiteDatabase sqLiteDatabase) {
26 String sql = "CREATE TABLE IF NOT EXISTS " + this.TABLE_NAME + " " +
27 "(id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, password TEXT, isEnabled INTEGER)";
28 sqLiteDatabase.execSQL(sql);
29 }
30
31 @Override
32 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
33 //do not touch this method
34 //it can destory your DataBase
35 }
36
37 //methods for adding a new user
38 //METHOD I - Worst method
39 public boolean addNewUser3(String userName, String password) {
40 //create sqk string -> INSERT INTO [table name] (field1,field2) VALUES ('string',number)
41 String sql = "INSERT INTO " + this.TABLE_NAME + " (userName,password) VALUES ('" + userName + "','" + password + "')";
42 db.execSQL(sql);
43 return true;
44 }
45
46 //METHOD II - we got and error but still it's complicated
47 public boolean addNewUser2(String userName, String password) {
48 //create sqk string -> INSERT INTO [table name] (field1,field2) VALUES ('string',number)
49 String sql = "INSERT INTO " + this.TABLE_NAME + " (userName,password) VALUES ('" + userName + "','" + password + "')";
50 SQLiteStatement sqLiteStatement = db.compileStatement(sql);
51 //we send the sql command, if we getting result of (-1) we got an error
52 long res = sqLiteStatement.executeInsert();
53 //if we got (-1) we got an error
54 return res != (-1);
55 }
56
57 //METHOD III - the best method
58 @Override
59 public boolean addNewUser(String userName, String password) {
60 if (!userExists(userName)) {
61 //create instance of Content Values
62 ContentValues myValues = new ContentValues();
63 //insert data inside content values
64 myValues.put("userName", userName);
65 myValues.put("password", password);
66 myValues.put("isEnabled", 1);
67 //put the values into table and getting a result which reflects record id (-1 indicates an error)
68 long res = db.insert(this.TABLE_NAME, null, myValues);
69 //return if all is ok
70 return res != (-1);
71 }
72 return false;
73 }
74
75
76 @Override
77 public boolean userExists(String userName) {
78 Cursor res = db.rawQuery("SELECT * FROM " + this.TABLE_NAME + " WHERE userName='" + userName + "'", null);
79 return res.getCount() > 0;
80 }
81
82 @Override
83 public boolean checkUser(String userName, String password) {
84 return false;
85 }
86
87 @Override
88 public boolean deleteUser(String userName) {
89 return false;
90 }
91
92 @Override
93 public boolean changePassword(String userName, String oldPassword, String newPassword) {
94 return false;
95 }
96}