Java 尝试重新打开已关闭的对象:SQLiteQuery
我知道这个问题已经被问过好几次了,但在我看来,其他答案都不适用于我的情况。我试图在按下按钮后删除数据库中的4项,但在尝试删除时,我得到了一个非法状态异常。我尝试用以下方式删除我的数据库,但似乎不起作用:Java 尝试重新打开已关闭的对象:SQLiteQuery,java,android,android-fragments,illegalstateexception,sqlite,Java,Android,Android Fragments,Illegalstateexception,Sqlite,我知道这个问题已经被问过好几次了,但在我看来,其他答案都不适用于我的情况。我试图在按下按钮后删除数据库中的4项,但在尝试删除时,我得到了一个非法状态异常。我尝试用以下方式删除我的数据库,但似乎不起作用: getActivity().deleteDatabase("TABLE_CATTLE"); 我还尝试使用循环逐个删除条目,如下所示: int cattle_i=0; int numCattle = cattleDb.getCattleCount(); while(cattle_i<nu
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++;
}
下面是GetBown方法:
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;
}
//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 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;
}
//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
关闭此行中的光标:
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;
}
您的GetCown
应该是:
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;
}
当我按照您的建议删除cursor.close()行时,仍然会得到一个IllegalStateException,但是这次它会说“无法执行此操作,因为连接池已关闭”。这是同一行。它在执行以下行时无法返回整数:return cursor.getCount();隐马尔可夫模型。。现在我得到一个游标索引越界异常?在我看来,它似乎是在这条线上出现的:牛=cattleDb.gettown(牛i);我在问题描述中添加了getBown方法。现在,我在deleteBown方法中得到一个空指针异常,在这里我调用了在我的BownBean类中定义的getID()方法。我包括了我的JavaBean类和DeleteBown方法。