Search code examples
javaandroidandroid-fragmentsillegalstateexceptionsqlite

attempt to re-open an already closed object: SQLiteQuery


I understand that this question has been asked several times but it seems to me that none of the other answers are applicable to my situation. I am trying to remove the 4 items in my database upon a button press, but I get an illegalstateexception when trying to do so. I have tried to delete my database in the following way, but it hasn't seemed to work:

getActivity().deleteDatabase("TABLE_CATTLE");

I have also tried using a loop to delete the entries one-by-one as such:

int cattle_i=0;
int numCattle = cattleDb.getCattleCount(); 
while(cattle_i<numCattle){
    Cattle cattle = cattleDb.getCattle(cattle_i);
    cattleDb.deleteCattle(cattle);
    cattle_i++;
}

Neither of these methods seem to be working for me. I only get the illegalstateexception when using the one-by-one approach. My code fails when calling the getCattleCount() method in my DatabaseHandler class, which I show below.

public int getCattleCount(){
    String countQuery = "SELECT  * FROM " + TABLE_CATTLE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();
    db.close();
    return cursor.getCount();
}

Here is the getCattle method:

Cattle getCattle(int id){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CATTLE,  new String[] 
            {KEY_ID,  KEY_TAG_ID, KEY_ALIAS, KEY_TIME, KEY_DATA}, KEY_ID + "=?",
            new String[] {String.valueOf(id) }, null, null, null, null);
    if(cursor!=null)
        cursor.moveToFirst();
    Cattle cattle = new Cattle(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1),
            cursor.getString(2),
            cursor.getString(3),
            cursor.getString(4));
    cursor.close();
    db.close();//test
    return cattle;
}  

deleteCattle method:

    //Deleting single cow
public void deleteCattle(Cattle cattle){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CATTLE, KEY_ID + " = ?",
            new String[] {String.valueOf(cattle.getID())});
    db.close();
}

Cattle Bean:

public class Cattle {

    // private variables
    int _id = 0;
    String _tag_id = null;
    String _alias = null;
    String _time_stamp = null;
    String _tag_data = null;
    String _user_email = null;

    // Empty Constructor
    public Cattle() {

    }

    // Constructor
    public Cattle(int id, String tag_id, String alias, String _time_stamp, String _tag_data) {
        this._id = id;
        this._tag_id = tag_id;
        this._alias = alias;
        this._time_stamp = _time_stamp;
        this._tag_data = _tag_data;
    }

    // Constructor
    public Cattle(String tag_id, String alias, String _time_stamp, String tag_data) {
        this._tag_id = tag_id;
        this._alias = alias;
        this._time_stamp = _time_stamp;
        this._tag_data = tag_data;
    }

    // getting ID
    public int getID() {
        return this._id;
    }

    // setting ID
    public void setID(int id) {
        this._id = id;
    }

    // getting tagID
    public String getTagID() {
        return this._tag_id;
    }

    // setting tagID
    public void setTagID(String tag_id) {
        this._tag_id = tag_id;
    }

    // getting alias
    public String getAlias() {
        return this._alias;
    }

    // setting alias
    public void setAlias(String alias) {
        this._alias = alias;
    }

    // getting time stamp
    public String getTimeStamp() {
        return this._time_stamp;
    }

    // setting time stamp
    public void setTimeStamp(String time_stamp) {
        this._time_stamp = time_stamp;
    }

    // getting tag data
    public String getTagData() {
        return this._tag_data;
    }

    // setting tag data
    public void setTagData(String tag_data) {
        this._tag_data = tag_data;
    }

}

edit: I used this tutorial to build my SQLiteDatabase http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/


Solution

  • You close the cursor in this line:

    cursor.close();
    

    And then try to use it in that one

    return cursor.getCount();
    

    This won't work as the cursor is already closed.

    You can rewrite your method this way:

    public int getCattleCount(){
        String countQuery = "SELECT  * FROM " + TABLE_CATTLE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int count = cursor.getCount();
        cursor.close();
        db.close();
        return count;
    }
    

    Your getCattle should be:

    Cattle getCattle(int id){
        Cattle cattle = null;
        SQLiteDatabase db = this.getReadableDatabase();
    
        Cursor cursor = db.query(TABLE_CATTLE,  new String[] 
                {KEY_ID,  KEY_TAG_ID, KEY_ALIAS, KEY_TIME, KEY_DATA}, KEY_ID + "=?",
                new String[] {String.valueOf(id) }, null, null, null, null);
        if(cursor!=null) {
            if (cursor.moveToFirst()) {
                cattle = new Cattle(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3),
                    cursor.getString(4));
            }
            cursor.close();
        }
        db.close();//test
        return cattle;
    }