1

I have written two apps. One is used to create data and store in a database, the other is used to display information from the first app's database. I need these two to be separate apps. The second app that displays the information has to make multiple calls to the first app's database. It's all working fine and well, but I can't close the database in the first app's content provider. I've read at Closing the database in a ContentProvider that I'm not supposed to which is fine. The problem is as I'm working on the second app, Eclipse's Logcat is just flooded with unclosed database messages. One message per query and I'm making multiple queries at a time.

Finally the question: Is there a way to either setup Eclipse to not display this certain error message, or filter the message out of the logcat?

Thanks TJ

Community
  • 1
  • 1
tjorlando
  • 75
  • 1
  • 14
  • 1
    the question you refer to is about closing the `database` not the cursor. You still need to close the cursor returned by your contentprovider, just like you would if your cursor came from the same app. – njzk2 Dec 19 '13 at 21:11
  • anytime you make a query you need to close the cursor after you are done with it, you never want to leave the cursor open – tyczj Dec 19 '13 at 21:16
  • I meant closing the database. I edited the question above – tjorlando Dec 19 '13 at 21:19
  • What error are you actually seeing then? – Nathaniel D. Waggoner Dec 19 '13 at 21:22
  • "android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here." The error is happening in the code of the first app, which is being called from the second app. The link in my question says I shouldn't close the database in the first app. – tjorlando Dec 19 '13 at 21:25
  • Post the full trace. I think you need to quadruple check that you're always closing your cursors, in both apps. – Nathaniel D. Waggoner Dec 19 '13 at 21:31

1 Answers1

1

EDIT:

Make sure you only call:

getReadableDatabase()

once, as per:

How to avoid db not close and cursor exception

Whenever you've finsihed pulling information from a cursor you need to call:

cursor.close();

So if you have some call:

public void parseValsFromCursor(Cursor C) {
  String val = c.getString(c.getColumnIndex(MYCOLUMN));
  Log.e(TAG,val);

 }

and then you try and do another db query, you'll see this error.

The fix is to modify the cursor parsing code to include the .close() method like so:

public void parseValsFromCursor(Cursor C) {
  String val = c.getString(c.getColumnIndex(MYCOLUMN));
  Log.e(TAG,val);
  c.close();
 }
Community
  • 1
  • 1
Nathaniel D. Waggoner
  • 2,856
  • 2
  • 19
  • 41
  • I meant closing the database in the first app's content provider. My mistake, I edited the question above. – tjorlando Dec 19 '13 at 21:20
  • I was calling "DbHelper = new DbAdapter(getContext()); DbHelper.open();" in my content provider every time it was called. I changed it to if (DbHelper == null) { DbHelper = new DbAdapter(getContext()); DbHelper.open();} Now it works fine. Thanks so much – tjorlando Dec 19 '13 at 21:41