Skip to content Skip to sidebar Skip to footer

Database Not Closed

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 n

Solution 1:

Isn't the database being opened twice?

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

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

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

Solution 2:

Look closely :) This

if(!db.isOpen()||db!=null) {
    db.close();db=null;
}

will try to close the db only if it is not open. Also, I think there's something wrong with the

||x!=null

part of both clauses.

In Java, the "double" logical operators are conditional. For || that means if the first argument is true then the second argument will not be evaluated at all, since logical OR will already be true no matter what. If it's not - then your link is checked for null. So, what you're doing is, you check whether cursor is closed. And if it is closed, you check the link for null - which obviously will be true given you succeeded calling any method on it, so no use of the second check.

I think the right clauses will be

if ( poisCursor != null && !poisCursor.isClosed()) // prevent NPE when calling .isClosed() in case poisCursor is null

and

if (db != null && db.isOpen()) // same thing + note the absence of negation

(You can read more on conditional operators in detail here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html)

Edit: @firebolt7 is right, you're losing reference to the object you get when calling open() in if clause.

Post a Comment for "Database Not Closed"