title: What to do with a database, retrieved by a SQLiteOpenHelper implementation, when finished using it?
body: <h2>Dear Fellow Android Developers!</h2>
  
  <p><br />
  <strong>EDIT:</strong></p>
  
  <p>Thank you all for your answers. I see from many of them that it seems to be common (and accepted) practice to write your own <code>close()</code> method in your database adapter. Fair enough. </p>
  
  <p>But how does that work with a <code>ContentProvider</code>? Usually when querying my database through my <code>ContentProvider</code> I simply issue something like:</p>
  
  <pre><code>Cursor managedCursor = managedQuery(...);
  </code></pre>
  
  <p>I don't see how I, with this methodology, can access the custom <code>close()</code> method in my custom <code>ContentProvider</code> implementation. Should I instead, from my <code>Activity</code>, do something like:</p>
  
  <pre><code>MyCustomProvider myProvider = (MyCustomProvider) getContentResolver();
  </code></pre>
  
  <p>and then:</p>
  
  <pre><code>myProvider.query(...);
  myProvider.close();
  </code></pre>
  
  <p>And above all; is this at all necessary (as of point 2 below)?</p>
  
  <p><strong>END EDIT</strong>
  <br />
  <br />
  <br />
  To a certain degree I must say that I get the concept of the <code>SQLiteOpenHelper</code>, what it is, how it's used and so. I even use it on a regular basis when I write my own <code>ContentProvider</code>'s.</p>
  
  <p>The thing is that I'm not sure what to do with the <code>SQLiteDatabase</code> object, returned by the <code>myOpenHelper.getWritableDatabase()</code> (or the <code>myOpenHelper.getReadableDatabase()</code> function for what matters) when I'm done with it.</p>
  
  <p>According to Android <code>ContentProvider.onCreate()</code> documentation:</p>
  
  <blockquote>
    <p>You should defer nontrivial initialization (such as opening, upgrading, and scanning     databases) until the content provider is used (via <code>query(Uri, String[], String, String[], String)</code>, <code>insert(Uri, ContentValues)</code>, etc).</p>
    
    <p>[...]</p>
    
    <p>If you do use <code>SQLiteOpenHelper</code>, make sure to avoid calling <code>getReadableDatabase()</code> or <code>getWritableDatabase()</code> from this method. (Instead, override <code>onOpen(SQLiteDatabase)</code> to initialize the database when it is first opened.)</p>
  </blockquote>
  
  <p>The above gives me a hint where to initialize the database (the <code>query(...)</code>, <code>insert(...)</code>, etc functions), but it doesn't tell me anything on how to treat the created <code>SQLiteDatbase</code> object when I've finished using it.</p>
  
  <ol>
  <li><p><em>Should I save it as a member variable of my <code>ContentProvider</code> implementation (and treat it much like a "private singleton" for future use)?</em></p></li>
  <li><p><em>Should I just leave it when exiting the <code>query(...)</code>, <code>insert(...)</code>, etc. functions and trust that the <code>SQLiteOpenHelper</code> will manage it for me in future calls?</em></p></li>
  <li><p><em>[Insert your alternative point-of-view here]</em></p></li>
  </ol>
  
  <p>Being the confiding (or lazy) developer I've implemented my code according to the second alternative above. But I can't get rid of the creepy feeling that I'm neglecting something important.</p>