SHARE
TWEET

sqllite class

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