试图重新打开一个已关闭的对象:sqlitequery [英] attempt to reopen an already-closed object: sqlitequery

查看:318
本文介绍了试图重新打开一个已关闭的对象:sqlitequery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我查询DB的两倍。我不明白的地方这个错误真的是从,因为我不关闭数据库的任何地方来了。在code,返回错误运行这样。我周围的检查,我只是看过像我这样的情况。

  BeaconHandler pullAllDB =新BeaconHandler(本);
    尝试{
        清单<&警号GT; beaconsShown = pullAllDB.getAllBeacons();
        对(BN最终烽火台:beaconsShown){
            尝试{
                INT messageCount = pullAllDB.getMessageCount();
                Log.d(信息,messageCount +消息找到);
                如果(messageCount大于0){
                    //做一点事
                }其他{
                    // 没做什么
                }
            }
            赶上(例外五){
                e.getStackTrace();
                Log.e(信息,e.getMessage());
            }
        }
    }

而code做查询...

 公众诠释getBeaconsCount(){    字符串countQuery =SELECT * FROM+ TABLE_BASIC_BEACON;
    SQLiteDatabase分贝= this.getReadableDatabase();
    光标光标= db.rawQuery(countQuery,NULL);
    cursor.close();    //返回计数
    返回cursor.getCount();}公众诠释getMessageCount(){    字符串mcountQuery =SELECT * FROM+ MESSAGE_BEACON;
    SQLiteDatabase MDB = this.getReadableDatabase();
    光标mcursor = mdb.rawQuery(mcountQuery,NULL);
    mcursor.close();    //返回计数
    返回mcursor.getCount();}


解决方案

您应该如果您收到错误张贴logcat中。它有助于了解哪些线是造成你的问题。

从Android文档。


  

关闭()


  
  

关闭游标,释放所有资源,使
  它完全无效的。


您调用 mcursor.getCount()您已关闭后,很可能导致错误

也许尝试这样的事情。

 诠释计数= mcursor.getCount();
mcursor.close();//返回计数
返回计数;

So essentially I am querying the DB twice. I don't understand where this error is really coming from because I am not closing the DB anywhere. The code that returns the error runs like this. I've checked around and I just having seen a case like mine.

BeaconHandler pullAllDB = new BeaconHandler(this);
    try {
        List<Beacon> beaconsShown = pullAllDB.getAllBeacons();
        for (final Beacon bn : beaconsShown) {
            try {
                int messageCount = pullAllDB.getMessageCount();
                Log.d("Message", messageCount + " Messages Found");
                if (messageCount > 0) {
                    //Do Something
                } else {
                    // Do Nothing
                }
            } 
            catch (Exception e) {
                e.getStackTrace();
                Log.e("Message", e.getMessage());
            }
        }
    }

And the code doing the queries...

public int getBeaconsCount() {

    String countQuery = "SELECT * FROM " + TABLE_BASIC_BEACON;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();

}

public int getMessageCount() {

    String mcountQuery = "SELECT * FROM " + MESSAGE_BEACON;
    SQLiteDatabase mdb = this.getReadableDatabase();
    Cursor mcursor = mdb.rawQuery(mcountQuery, null);
    mcursor.close();

    // return count
    return mcursor.getCount();

}

解决方案

You should post a logcat if you are getting an error. It helps to see which line is causing your problem.

From the Android docs.

close()

Closes the Cursor, releasing all of its resources and making it completely invalid.

Your call to mcursor.getCount() after you have closed it is likely causing the error

Maybe try something like this.

int count = mcursor.getCount();
mcursor.close();

// return count
return count ;

这篇关于试图重新打开一个已关闭的对象:sqlitequery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆