尝试重新打开已经关闭的对象:SQLiteQuery

时间:2013-11-29 00:58:10

标签: java android android-fragments illegalstateexception sqlite

我知道这个问题已被多次询问,但在我看来,其他答案都不适用于我的情况。我按下按钮时试图删除数据库中的4个项目,但是在尝试这样做时我得到了非法的例句。我试图以下列方式删除我的数据库,但它似乎没有工作:

getActivity().deleteDatabase("TABLE_CATTLE");

我还尝试使用循环逐个删除条目:

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

这些方法似乎都不适合我。在使用逐一方法时,我只得到非法状态。在我的DatabaseHandler类中调用getCattleCount()方法时,我的代码失败了,我将在下面显示。

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

这是getCattle方法:

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方法:

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

牛豆:

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

}

编辑:我用这个教程来构建我的SQLiteDatabase http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

1 个答案:

答案 0 :(得分:1)

关闭此行中的光标:

cursor.close();

然后尝试在那个

中使用它
return cursor.getCount();

这不起作用,因为光标已经关闭。

您可以这样重写您的方法:

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

您的getCattle应该是:

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