title: How to Open/Close SQLite db in Android Properly
body: <p>I have an app that functions properly and does not force close or crash.  But when I look at LogCat, it occasionally gives me this:</p>
  
  <pre><code>05-20 15:24:55.338: E/SQLiteDatabase(12707): close() was never explicitly called on database '/data/data/com.---.--/databases/debt.db' 
  05-20 15:24:55.338: E/SQLiteDatabase(12707): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
  </code></pre>
  
  <p>a little ways down...</p>
  
  <pre><code>05-20 15:24:55.338: E/System(12707): Uncaught exception thrown by finalizer
  05-20 15:24:55.338: E/System(12707): java.lang.IllegalStateException: Don't have database lock!
  </code></pre>
  
  <p>I am not sure when I should be opening and closing my Database?</p>
  
  <p>I have a Main activity that is simply a splash screen.  It then goes into an activity that calls a ListView using info from the DB; so it is at <em>this</em> activity where the DB is first opened. </p>
  
  <p>There is also one other Activity where the DB is required that branches off the one with the ListVeew.  When am I supposed to be opening and closing this?  Word seems to be that I simply need to open once, and then close when the app is "paused", "stopped" or "destroyed".</p>
  
  <p>If this is the case, where do I put the db.close() method... in the Splash Screen Main Activity where onStop, etc is located? or the same Activity as the one that opens the DB?  or.. is there another place?</p>
  
  <p>UPDATE:</p>
  
  <p>This is the line in code that the error keeps pointing to:</p>
  
  <pre><code>public void open() throws SQLException {
      database = dbHelper.getWritableDatabase();
  }
  </code></pre>