Reputation: 301
I am using the following routine in my database adapter. Everytime it is called, an error is created with a "Application did not close the cursor".
I don't see how I can close the cursor other then how I am, since I am not opening it in the calling routine. This is used within a display adapter within a list.
I am calling it with:
int cnt = mDbHelper.dbio_rcount( "select count(*) from mytable where field1 = 'V' ));
public int dbio_rcount( String p_query )
{
int v_ret = 0 ;
Cursor mCursor = null ;
try
{
mCursor = mDb.rawQuery( p_query, null );
}catch (SQLException e) {}
if ( ( mCursor != null )
&& ( mCursor.moveToFirst()) )
{
v_ret = mCursor.getInt( 0 );
}
mCursor.close();
return v_ret ;
}
Upvotes: 0
Views: 1421
Reputation: 9284
My guess is that you are getting an exception with the mCursor.moveToFirst() call which means your app is crashing before mCursor.close() happens.. Usually what I do is check that mCursor.getCount() > 0 before I call moveToFirst().. But that's just me.. I would suggest this:
public int dbio_rcount(String p_query)
{
int v_ret = 0 ;
Cursor mCursor = null ;
try
{
mCursor = mDb.rawQuery(p_query, null);
if (mCursor != null && mCursor.getCount() > 0)
{
mCursor.moveToFirst();
v_ret = mCursor.getInt( 0 );
}
} catch (SQLException e) {
Log.e(TAG, "sql exception in dbio_count", e);
} catch(Exception ex) {
Log.e(TAG, "other exception in dbio_count", ex);
} finally {
if (mCursor != null) {
mCursor.close();
}
}
return v_ret ;
}
Upvotes: 5