SQLiteDatabase错误,无益日志 [英] SQLiteDatabase Error, unhelpful Log

查看:435
本文介绍了SQLiteDatabase错误,无益日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发表我的应用程序的更新和我得到一吨的错误,从用户和我不能重新创建或针点的问题。

两个错误,我越来越: java.lang.IllegalStateException:尝试重新打开一个已关闭的对象:SQLiteDatabase

java.lang.IllegalStateException:数据库无法打开

在错误的地方发生在以下code:

 公共DBUserC的getUser(INT _id){
        同步(DBAdapter.LOCK){
            M.openDB(上下文);
            光标光标= M.database.query(DBUserHelper.TABLE_USERS,
                    VUser.allColumns,DBUserHelper.COLUMN_ID +=+ _id +',NULL,NULL,NULL,NULL);
            DBUserC用户;
            如果(cursor.moveToFirst()){
                用户= cursorToInfo(光标);
            } 其他{
                用户= NEWUSER();
            }
            cursor.close();
            M.closeDB();
            返回用户;
        }
        }
 

在这个版本中,有一个数据库的升级,在此我通过执行 db.execSql 三个查询。它不是在单独的线程。

在每次调用数据库(除了在 onUpgrade ),我同步,然后打开它,我跑code,然后将其关闭。我有没有问题,直到此升级并不能找到这个问题。

任何帮助将是很大的AP preciated。

编辑:打开我的databas,我做的:

 如果(辅助== NULL)
    辅助=新DBAdapter(上下文);
如果(数据库== NULL){
     数据库= helper.getWritableDatabase();
}否则,如果(!database.isOpen())
     数据库= helper.getWritableDatabase();
 

和关闭:

  helper.close();
帮助= NULL;
数据库= NULL;
 

例方法获得信息:

 公共DBUserC的getUser(INT _id){
        同步(DBAdapter.LOCK){
            openDB(背景); //这是开放的code以上
            光标光标= M.database.query(DBUserHelper.TABLE_USERS,
                    VUser.allColumns,DBUserHelper.COLUMN_ID +=+ _id +',NULL,NULL,NULL,NULL);
            DBUserC用户;
            如果(cursor.moveToFirst()){
                用户= cursorToInfo(光标); //不包含数据块的操作
            } 其他{
                用户= NEWUSER(); ////不包含数据库操作
            }
            cursor.close();
            closeDB(); //这是接近code以上
            返回用户;
        }
    }
 

AHost.onCreate

 保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        M.initializeDB(上下文); //为openDB code我上面贴同步调用
        M.openDataDB(上下文); //打开一个不同的数据库,通过不同的文件名。这个数据库是不是一个问题

        //无关的UI设置
        INT _id = pref.getInt(P.eLastUser,VUser.ID_NEW);用户//行ID

        M.requeryUser(); //同步,访问数据库
        M.switchUser(_id); //同步,访问数据库

    }
 

解决方案

我无法重现的问题,因为在测试中,我是从DB版本3升级到4。我的很多用户的版本从版本2到4个版本进行了升级。一些code代表从2至3升级是基于方法。我最终改变其中的一些方法,第4版,这打破了第3版升级。然后,我是从一个cursorToObject()方法我有,这引起了database.close要跳过得到一个捕获异常,然后我得到了sqlite的异常

I released an update of my app and am getting a ton of errors from users and I can't recreate it or pin-point the problem.

Two errors I'm getting: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase

java.lang.IllegalStateException: database not open

The error is happening somewhere in the following code:

public DBUserC getUser(int _id){
        synchronized (DBAdapter.LOCK){
            M.openDB(context);
            Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
                    VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
            DBUserC user;
            if (cursor.moveToFirst()){
                user = cursorToInfo(cursor);
            } else{
                user = newUser();
            }
            cursor.close();
            M.closeDB();
            return user;
        }
        }

In this version, there is a database upgrade, in which I perform three queries via db.execSql. It is not in a separate thread.

On every call to the database (except in the onUpgrade), I synchronize, then open it, run my code, then close it. I was having no problems until this upgrade and can't find the issue.

Any help would be greatly appreciated.

EDIT: To open my databas, I do:

if (helper==null)
    helper = new DBAdapter(context);
if (database==null){
     database = helper.getWritableDatabase();
} else if (!database.isOpen())
     database = helper.getWritableDatabase();

and to close:

helper.close();
helper = null;
database = null;

Example method for getting info:

    public DBUserC getUser(int _id){
        synchronized (DBAdapter.LOCK){
            openDB(context);//this is the open code above
            Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
                    VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
            DBUserC user;
            if (cursor.moveToFirst()){
                user = cursorToInfo(cursor);//does not contain DB operations
            } else{
                user = newUser(); ////does not contain Database operations
            }
            cursor.close();
            closeDB();//This is the close code above
            return user;
        }
    }

AHost.onCreate

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        M.initializeDB(context); //synchronized call to the openDB code I posted above
        M.openDataDB(context); //opens a different database by a different file name. This DB is not an issue

        //irrelevant ui setup
        int _id = pref.getInt(P.eLastUser, VUser.ID_NEW);//row id of user

        M.requeryUser();//synchronized, access database
        M.switchUser(_id);//synchronized, access database

    }

解决方案

I could not recreate issue because in testing, I was upgrading from DB version 3 to version 4. Many of my users were upgrading from version 2 to version 4. Some of the code for upgrading from 2 to 3 was method-based. I ended up changing some of those methods for version 4, which broke the version 3 upgrade. I then was getting a caught exception from a cursorToObject() method I had, which caused the database.close to be skipped and then I got the sqlite exception

这篇关于SQLiteDatabase错误,无益日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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