· 7 years ago · Jan 17, 2019, 11:44 AM
1package com.repoai.android.mysmarthouse;
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.Base64;
10import android.util.Log;
11
12public class UserSQLlite extends SQLiteOpenHelper implements UserAble {
13 Context context;
14 //name of the DB file in our phone
15 public static final String DB_NAME="smartHouse.db";
16 //name of the table
17 String table_name;
18 //an instance to our SqliteDatabase
19 SQLiteDatabase db;
20
21 public UserSQLlite(Context context) { //String table_name
22 super(context,DB_NAME,null,1);
23 this.context = context;
24 //this.table_name=table_name;
25 //set our data base to read and write mode.
26 db=this.getWritableDatabase();
27 }
28
29 @Override
30 public boolean createUser(String userName, String password) {
31 //method I - the good one
32
33 //create instance of ContentValues to hold our values
34 ContentValues myValues = new ContentValues();
35 //insert data by key and value
36 String encUserName= Base64.encodeToString(userName.getBytes(),0);
37 String encUserPass=Base64.encodeToString(password.getBytes(),0);
38 Log.e("enc", "createUser: "+encUserName );
39 Log.e("enc", "createUser: "+encUserPass );
40 myValues.put("userName",encUserName);
41 myValues.put("password",encUserPass);
42 //putting our values into table and getting a result which reflact record id
43 //if we get -1, we had an error
44 long res=db.insert("users",null,myValues);
45 //return true if we not get -1, error
46 return res!=(-1);
47
48 /*
49 //method II
50 //create sql string - INSERT INTO [Table name] (field name,field name) values (value 1, value2)
51 String sql = "INSERT INTO "+this.table_name+" (userName,password) values ("+userName+","+password+")";
52 //we are compilng the string to sql string to SQL statment
53 SQLiteStatement statement =db.compileStatement(sql);
54 //we send the sql command, result of (-1) is an error
55 long res2=statement.executeInsert();
56 //return res2!=(-1);
57
58 //method III
59 //create sql string - INSERT INTO [Table name] (field name,field name) values (value 1, value2)
60 String sql2 = "INSERT INTO "+this.table_name+" (userName,password) values ("+userName+","+password+")";
61 db.execSQL(sql2);
62 return true;
63 */
64 }
65
66 public boolean createRoom(String roomName, String roomDescription, Boolean isActive,
67 int numOfDevices, boolean present)
68 {
69 //create instance of contentValues
70 ContentValues myValues = new ContentValues();
71 myValues.put("roomName", roomName);
72 myValues.put("roomDescription",roomDescription);
73 myValues.put("isActive",isActive?1:0);
74 myValues.put("numOfDevices",numOfDevices);
75 myValues.put("present",present?1:0);
76 //put our data into the DB
77 long res=db.insert("rooms",null,myValues);
78 return res!=(-1);
79 }
80
81 public Cursor getAllRooms()
82 {
83 //using Cursor to hold our raw data (Cursor is a collection type object)
84 Cursor res =db.rawQuery("SELECT * FROM rooms",null);
85 return res;
86 }
87
88 public Cursor getAllDevice(int roomID)
89 {
90 Cursor res = db.rawQuery("SELECT * FROM devices WHERE roomID="+roomID,null);
91 return res;
92 }
93
94 public void deleteDevice(int deviceID)
95 {
96 db.execSQL("DELETE FROM devices WHERE deviceID="+deviceID);
97 }
98
99
100
101 @Override
102 public boolean userExists(String userName) {
103 return false;
104 }
105
106 @Override
107 public boolean checkUser(String userName, String password) {
108 return false;
109 }
110
111 @Override
112 public void onCreate(SQLiteDatabase db) {
113 //create user table
114 String sqlStatment="CREATE TABLE IF NOT EXISTS users ";
115 sqlStatment+= "(id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT , password TEXT)";
116 db.execSQL(sqlStatment);
117
118 //create rooms table
119 String roomStatemt="CREATE TABLE IF NOT EXISTS rooms ";
120 roomStatemt+="(id INTEGER PRIMARY KEY AUTOINCREMENT, roomName TEXT, roomDescription TEXT, ";
121 roomStatemt+="isActive INTEGER, numOfDevices INTEGER, present INTEGER)";
122 db.execSQL(roomStatemt);
123
124 }
125
126 @Override
127 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
128 //do not touch!!!!!
129 //db.execSQL("DROP TABLE IF EXISTS "+this.table_name);
130 }
131}