Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attempt to reopen an already-closed object: sqlitequery

So essentially I am querying the DB twice. I don't understand where this error is really coming from because I am not closing the DB anywhere. The code that returns the error runs like this. I've checked around and I just having seen a case like mine.

BeaconHandler pullAllDB = new BeaconHandler(this);
    try {
        List<Beacon> beaconsShown = pullAllDB.getAllBeacons();
        for (final Beacon bn : beaconsShown) {
            try {
                int messageCount = pullAllDB.getMessageCount();
                Log.d("Message", messageCount + " Messages Found");
                if (messageCount > 0) {
                    //Do Something
                } else {
                    // Do Nothing
                }
            } 
            catch (Exception e) {
                e.getStackTrace();
                Log.e("Message", e.getMessage());
            }
        }
    }

And the code doing the queries...

public int getBeaconsCount() {

    String countQuery = "SELECT * FROM " + TABLE_BASIC_BEACON;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();

}

public int getMessageCount() {

    String mcountQuery = "SELECT * FROM " + MESSAGE_BEACON;
    SQLiteDatabase mdb = this.getReadableDatabase();
    Cursor mcursor = mdb.rawQuery(mcountQuery, null);
    mcursor.close();

    // return count
    return mcursor.getCount();

}
like image 532
TheHamstring Avatar asked Sep 19 '13 23:09

TheHamstring


2 Answers

You should post a logcat if you are getting an error. It helps to see which line is causing your problem.

From the Android docs.

close()

Closes the Cursor, releasing all of its resources and making it completely invalid.

Your call to mcursor.getCount() after you have closed it is likely causing the error

Maybe try something like this.

int count = mcursor.getCount();
mcursor.close();

// return count
return count ;
like image 86
Nicholas Avatar answered Sep 20 '22 05:09

Nicholas


I'm assuming here that pullAllDB is your database object which contains the code doing the queries. In that case, before the line, List<Beacon> beaconsShown = pullAllDB.getAllBeacons();, you should do something like pullAllDB.open(); and do pullAllDB.close(); after you are done running queries.

So all in all, your function would look like..

try {
    //open the database class here
    pullAllDB.open();

    List<Beacon> beaconsShown = pullAllDB.getAllBeacons();
    for (final Beacon bn : beaconsShown) {
        try {
            int messageCount = pullAllDB.getMessageCount();
            Log.d("Message", messageCount + " Messages Found");
            if (messageCount > 0) {
                //Do Something
            } else {
                // Do Nothing
            }
        } 
        catch (Exception e) {
            e.getStackTrace();
            Log.e("Message", e.getMessage());
        }

    //close the database here
    pullAllDB.close();
    }
}
like image 29
Razgriz Avatar answered Sep 19 '22 05:09

Razgriz