attempt to reopen an already-closed object: sqlitequery

 ̄綄美尐妖づ 提交于 2019-12-07 02:22:00

问题


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

}

回答1:


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 ;



回答2:


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


来源:https://stackoverflow.com/questions/18906571/attempt-to-reopen-an-already-closed-object-sqlitequery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!