Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.repoai.android.mysmarthouse;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteStatement;
- import android.util.Base64;
- import android.util.Log;
- public class UserSQLlite extends SQLiteOpenHelper implements UserAble {
- Context context;
- //name of the DB file in our phone
- public static final String DB_NAME="smartHouse.db";
- //name of the table
- String table_name;
- //an instance to our SqliteDatabase
- SQLiteDatabase db;
- public UserSQLlite(Context context) { //String table_name
- super(context,DB_NAME,null,1);
- this.context = context;
- //this.table_name=table_name;
- //set our data base to read and write mode.
- db=this.getWritableDatabase();
- }
- @Override
- public boolean createUser(String userName, String password) {
- //method I - the good one
- //create instance of ContentValues to hold our values
- ContentValues myValues = new ContentValues();
- //insert data by key and value
- String encUserName= Base64.encodeToString(userName.getBytes(),0);
- String encUserPass=Base64.encodeToString(password.getBytes(),0);
- Log.e("enc", "createUser: "+encUserName );
- Log.e("enc", "createUser: "+encUserPass );
- myValues.put("userName",encUserName);
- myValues.put("password",encUserPass);
- //putting our values into table and getting a result which reflact record id
- //if we get -1, we had an error
- long res=db.insert("users",null,myValues);
- //return true if we not get -1, error
- return res!=(-1);
- /*
- //method II
- //create sql string - INSERT INTO [Table name] (field name,field name) values (value 1, value2)
- String sql = "INSERT INTO "+this.table_name+" (userName,password) values ("+userName+","+password+")";
- //we are compilng the string to sql string to SQL statment
- SQLiteStatement statement =db.compileStatement(sql);
- //we send the sql command, result of (-1) is an error
- long res2=statement.executeInsert();
- //return res2!=(-1);
- //method III
- //create sql string - INSERT INTO [Table name] (field name,field name) values (value 1, value2)
- String sql2 = "INSERT INTO "+this.table_name+" (userName,password) values ("+userName+","+password+")";
- db.execSQL(sql2);
- return true;
- */
- }
- public boolean createRoom(String roomName, String roomDescription, Boolean isActive,
- int numOfDevices, boolean present)
- {
- //create instance of contentValues
- ContentValues myValues = new ContentValues();
- myValues.put("roomName", roomName);
- myValues.put("roomDescription",roomDescription);
- myValues.put("isActive",isActive?1:0);
- myValues.put("numOfDevices",numOfDevices);
- myValues.put("present",present?1:0);
- //put our data into the DB
- long res=db.insert("rooms",null,myValues);
- return res!=(-1);
- }
- public Cursor getAllRooms()
- {
- //using Cursor to hold our raw data (Cursor is a collection type object)
- Cursor res =db.rawQuery("SELECT * FROM rooms",null);
- return res;
- }
- public Cursor getAllDevice(int roomID)
- {
- Cursor res = db.rawQuery("SELECT * FROM devices WHERE roomID="+roomID,null);
- return res;
- }
- public void deleteDevice(int deviceID)
- {
- db.execSQL("DELETE FROM devices WHERE deviceID="+deviceID);
- }
- @Override
- public boolean userExists(String userName) {
- return false;
- }
- @Override
- public boolean checkUser(String userName, String password) {
- return false;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- //create user table
- String sqlStatment="CREATE TABLE IF NOT EXISTS users ";
- sqlStatment+= "(id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT , password TEXT)";
- db.execSQL(sqlStatment);
- //create rooms table
- String roomStatemt="CREATE TABLE IF NOT EXISTS rooms ";
- roomStatemt+="(id INTEGER PRIMARY KEY AUTOINCREMENT, roomName TEXT, roomDescription TEXT, ";
- roomStatemt+="isActive INTEGER, numOfDevices INTEGER, present INTEGER)";
- db.execSQL(roomStatemt);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- //do not touch!!!!!
- //db.execSQL("DROP TABLE IF EXISTS "+this.table_name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement