Search code examples
androidsqlitekotlinsqliteopenhelper

How do I work with same SQLiteOpenHelper object in multiple activities in kotlin


I am making an app for my university project and I have stumbled upon a problem. I want to work with a DatabaseHelper that inherits SQLiteOpenHelper object that I have instantiated in activity1, in activity2. I have searched the web for a while and haven't found a solution. I also tried inheriting DatabaseHelper with Serializable and putting it as extra when starting a new activity, but that didn't seem to work. It is very important that I use the same DatabaseHelper object so I get the same users in activity2, because in it I will show different elements considering who the user is, and put a personalized message. Here is the code for my DatabaseHelper


class DatabaseHelper(context: Context):SQLiteOpenHelper(context,dbname,factory, version),Serializable{
    companion object{
        internal val dbname="database"
        internal val factory=null
        internal val version=1
    }

    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL("CREATE TABLE users(email VARCHAR(30) PRIMARY KEY,password VARCHAR(10))")
        db?.execSQL("CREATE TABLE machines(name VARCHAR(30) PRIMARY KEY, programmes VARCHAR(10), available CHAR(1))")
        db?.execSQL("CREATE TABLE isUsing(machineID REFERENCES machines(id),userID REFERENCES users(email), PRIMARY KEY (machineID,userID))")
    }

    fun userPresent(email:String,password: String):Boolean{
        val db=writableDatabase
        val query="SELECT * FROM users WHERE email='$email' AND password='$password'"
        val cursor=db.rawQuery(query,null)
        if(cursor.count<=0){
            cursor.close()
            return false
        }
        cursor.close()
        return true
    }

    fun insertMachine(name:String, programmes:String, available:String){
        val db: SQLiteDatabase=writableDatabase
        val values: ContentValues= ContentValues()
        values.put("available",available)
        values.put("name",name)
        values.put("programmes",programmes)
        db.insert("machines",null,values)
        db.close()
    }

    fun insertUserData(email:String,password:String){
        val db: SQLiteDatabase=writableDatabase
        val values: ContentValues= ContentValues()
        values.put("email",email)
        values.put("password",password)
        db.insert("users",null,values)
        db.close()
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        TODO("Not yet implemented")
    }
}

If I try to pass instance of a DatabaseHelper like this

val intent= Intent(it,AdminMainActivity::class.java)
            var args:Bundle= Bundle()
            args.putSerializable("dbHelper",dbHelper as Serializable)
            startActivity(intent)

and get it in a new activity like this

var bundle=intent.extras
handler= bundle!!.getSerializable("dbHelper") as DatabaseHelper

I get java.lang.NullPointerException on a line with handler= bundle!!.getSerializable("dbHelper") as DatabaseHelper


Solution

  • It is very important that I use the same DatabaseHelper object so I get the same users in activity2

    You don't need to use the same object instance. It's the underlying database data that matters and it's the same for all instances of the helper class.

    So, trying to pass around an object is not really the right problem to solve. A better approach is just to instantiate DatabaseHelper wherever you need it.