Issue
In my Android Activity I need to read things from a database. I've created a helper class which deals with my database access. This helper class has several different methods designed to read different things and return them without much fuss. Here is one example.
public String GetName(int id)
{
String toReturn = null;
Cursor cursor = null;
try
{
cursor = db.query(TABLE_NAME,
new String[] {"name"},
"id = " + id,
null,
null,
null,
null,
null);
assert(cursor.getCount() == 1);
cursor.moveToFirst();
toReturn = new String(cursor.getString(0));
}
finally
{
if(cursor != null)
cursor.close();
}
return toReturn;
}
I keep getting the error
10-10 15:52:18.991: W/SQLiteCompiledSql(28313): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT name FROM my_table WHERE id = 0 10-10 15:52:18.991: W/SQLiteCompiledSql(28313): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
with a stack trace that leads right to this function (and ones very similar to it), although the Activity works fine. What am I doing wrong? I looked around here for a while, and that's when I added the try/finally to make sure I always close the Cursor, but I'm still getting the error. How can I fix this?
Solution
You are closing cursor correctly but you also need to close the database, make sure that you are closing the database also:
if(db.isOpen()) db.close();
Answered By - Asif Mujteba
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.