数据库未关闭 [英] Database not closed

查看:243
本文介绍了数据库未关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了有关此问题的其他问题并尝试了解决方案,但不幸的是,它并没有帮助我.

I looked at other questions regarding this problem and tried their solutions, but it did not help me unfortunately.

问题不是关键性问题,这意味着我的程序不会崩溃,它会继续运行,但是无论如何我都希望没有错误.

The problem is not of critical nature, meaning my program does not crash, it continues running, but anyway I would like not to have errors if possible.

我正在使用 AsyncTask 类创建一些对象并将其加载到数组中.我使用的数据库是从网上获得的文件,因此我没有DBhelper类.我在AsyncTask的doInBackround内部打开数据库.在其中,我有try-catch来完成所有与数据库相关的工作.

I am using AsyncTask class to create some objects and load them up into an array. Database I use is a file obtained from the net, therefore I have no DBhelper class. I open the DB inside of doInBackround of AsyncTask. Inside it I have try-catch where I do all my db related work.

起初,我在try语句中具有 db cursors 的变量,并且还关闭了 db cursors 放在最后.我发现有一个链接说应该使用try-catch-final,所以我这样做了,我将变量声明移到了try-catch-final语句的外部,并管理了 db cursor的关闭 final 部分中的em>变量,但仍然无济于事,错误仍然存​​在.

At first I had variables for db and cursors inside of the try statement and also I had closure of db and cursors in it at the end. I found one link saying try-catch-final should be used, so I did that, I moved variable declaration outsite of try-catch-final statement and managed closing of db and cursor variables in the final part, but still it does not help, errors are still here.

这是一些错误,我不会全部C/P:

Here are some errors, I won't C/P all of them:

01-25 17:22:45.142:E/Database(333):close()从未在数据库'/data/data/stet.cityapp/app_databases/baza.db中明确调用

01-25 17:22:45.142: E/Database(333): close() was never explicitly called on database '/data/data/stet.cityapp/app_databases/baza.db

01-25 17:22:45.142:E/Database(333):android.database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象

01-25 17:22:45.142: E/Database(333): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

01-25 17:22:45.142:E/Database(333):位于stetocina.cityapp.SplashScreen $ LoadDB.doInBackground(SplashScreen.java:1)

01-25 17:22:45.142: E/Database(333): at stetocina.cityapp.SplashScreen$LoadDB.doInBackground(SplashScreen.java:1)

01-25 17:22:45.142:E/Database(333):位于java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306)

01-25 17:22:45.142: E/Database(333): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

01-25 17:22:45.142:E/Database(333):在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581)

01-25 17:22:45.142: E/Database(333): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)

代码概述:

protected Boolean doInBackground(String... params) {
SQLiteDatabase db = null;
Cursor poisCursor = null, tmpCursor = null;
String whereString;
try {               
    String DBpath = getDir("databases", 0).getAbsolutePath() + File.separator + "baza.db";
    if (SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.OPEN_READONLY) == null) return false;
    db =  SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    
    ...
    
    result = true;
} catch (Exception e) {
    result = false;
    return result;
} finally {
    if (!poisCursor.isClosed() || poisCursor != null) {
        poisCursor.deactivate();
        poisCursor.close();
        poisCursor = null;
    }
    if (!db.isOpen() || db != null) {
        db.close();
        db = null;
    }
}
return result;

}

我在AsyncTask中执行此操作是否会遇到一些问题?在AsyncTaks的 onPostExecute 方法中,如果一切顺利并且结果为true,则我将打开一个新的Activity,在调试时,我注意到该错误仅在显示新活动时才会显示,而退出后不会立即显示来自doInBackground.

Could there be some problem that I am doing it in an AsyncTask? In the onPostExecute methond of AsyncTaks, if everything went well and result is true, I open a new Activity, and while debugging I noticed that the error is displayed only when new activity is displayed and not immediatelly after exit from doInBackground.

感谢您的帮助!

推荐答案

不是两次打开数据库吗?

Isn't the database being opened twice?

  1. 在'if'条件内(如果if(SQLiteDatabase.openDatabase(DBpath,null,SQLiteDatabase.OPEN_READONLY)== null)返回false;)

  1. inside the 'if' condition (if (SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.OPEN_READONLY) == null) return false;)

在下一行(db = SQLiteDatabase.openDatabase(DBpath,null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);)

in the next line (db = SQLiteDatabase.openDatabase(DBpath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);)

并且您仅关闭第二个数据库(db).

and you are closing only the second one(db).

这篇关于数据库未关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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