我在下面的函数中收到错误,这不是一个常量错误。它经常发生,但错误是'java.lang.IllegalStateException:无法执行此操作,因为连接池已经
** if(cursor.moveToFirst()){**
错误public static DatabaseItem getArticle(String title) {
SQLiteDatabase db = mActivity.getWritableDatabase();
String query = "SELECT * FROM " + Database.DATABASE_TABLE_ARTICLES +
" WHERE " + FEED_TITLE + "=\"" + title + "\"";
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
DatabaseItem item = new DatabaseItem(
cursor.getInt(0), // ID
cursor.getString(1), // NAME
cursor.getString(2), // TITLE
cursor.getString(3), // DESCRIPTION
cursor.getString(4), // LINK
cursor.getString(5), // TIME
cursor.getString(6), // THUMBNAIL
cursor.getString(7), // ARTICLE IMAGE URL
cursor.getBlob(8), // ARTICLE IMAGE BYTE
cursor.getString(9) // ARTICLE TEXT
);
return item;
} while (cursor.moveToNext());
}
return null;
}
答案 0 :(得分:0)
您应该关闭Cursor对象以释放一些资源。这里最简单的方法是将你的if包装在try / finally块中:
try {
if (cursor.moveToFirst()) {
// blah blah blah
}
return null;
} finally {
cursor.close();
}
此处还有其他一些问题: