attempt to reopen an already-closed object: sqlitequery
Asked Answered
C

2

8

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();

}
Campo answered 19/9, 2013 at 23:27 Comment(0)
I
22

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 ;
Inflectional answered 20/9, 2013 at 0:2 Comment(1)
Wow, I can't believe I didn't pick up on that myself. I guess sometimes you just need another set of eyes.Campo
E
1

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();
    }
}
Embow answered 20/9, 2013 at 0:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.