您好,我正在尝试从android中的本地数据库查询数据。 1:m关系中有两个类别:Category和News。我想根据其类别ID搜索新闻列表。但是,当我这样做时,我会遵循以下异常。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infiniteloop.newsmobile/com.infiniteloop.newsmobile.NewsList}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.infiniteloop.newsmobile/databases/NEWS_DB
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.infiniteloop.newsmobile/databases/NEWS_DB
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1312)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.infiniteloop.newsmobile.db.NewsDBAdapter.getNewsByCategory(NewsDBAdapter.java:105)
at com.infiniteloop.newsmobile.data_models.NewsManager.getNewsByCategory(NewsManager.java:100)
这是我在NewsDBAdapter中的查询。
public Cursor getNewsByCategory(int Cat_ID){
Cursor c = db.rawQuery("SELECT * FROM " + TABLE_NEWS + " WHERE " +
NEWS_CATEGORY_ID + " = " + Cat_ID + ";", null);
return c;
}
我正在另一个类中调用此方法
public List<News> getNewsByCategory(int categoryID){
List<News> newsList = new ArrayList<>();
Cursor c = null;
try {
if(!adapter.isDBOpen()){
adapter.openDB();
}
c = adapter.getNewsByCategory(categoryID);
Log.i(TAG, "getNewsByCategory: " + c);
if (c.moveToFirst()) {
do {
int News_ID = c.getInt(c.getColumnIndex(NEWS_ID));
String News_TITLE = c.getString(c.getColumnIndex(NEWS_TITLE));
String News_Content = c.getString(c.getColumnIndex(NEWS_CONTENT));
String News_Date = c.getString(c.getColumnIndex(NEWS_DATE));
String News_IMGURL = c.getString(c.getColumnIndex(NEWS_IMGURL));
int News_Category_ID = c.getInt(c.getColumnIndex(NEWS_CATEGORY_ID));
News news = new News(News_ID, News_TITLE, News_Content, News_Date, News_IMGURL, News_Category_ID);
newsList.add(news);
} while (c.moveToNext());
}
return newsList;
}finally {
if (c!=null){
c.close();
}
if(adapter.isDBOpen()){
adapter.closeDB();
}
}
}
答案 0 :(得分:0)
我认为这与该帖子有关:Attempt to reopen an already-closed object sqlitedatabase
但是您没有使用数据库的可读实例。 尝试以下代码:
public Cursor getNewsByCategory(int Cat_ID){
SQLiteDatabase db = adapter.getReadableDatabase(); // You need to call
Cursor c = db.rawQuery("SELECT * FROM " + TABLE_NEWS + " WHERE " +
NEWS_CATEGORY_ID + " = " + Cat_ID + ";", null);
return c;
}