title: Why does SQLiteOpenHelper cause IllegalArgumentException database not open?
body: <p>First of all, all similar postings here didn't help. </p>
  
  <p>In an AsyncTask I check if the app startup is the very first startup at all, that means I check if there is a database at all. I want to do that by query from a table. This is what is being executed:</p>
  
  <pre><code>@Override
  public List&lt;Entity&gt; loadAll(Entity markerEntity)
  {
      Log.i(TAG, "trying to load all entities of type " + markerEntity.getTable());
  
      List&lt;Entity&gt; results = new LinkedList&lt;Entity&gt;();
  
      SQLiteDatabase db = defaultSQLiteOpenHelper.getWritableDatabase();
  
      Cursor cursor = db.query(markerEntity.getTable(), markerEntity.getAllColumns(), null, null, null, null, null);    
      cursor.moveToFirst();
  
      while (!cursor.isAfterLast())
      {
          Entity result = markerEntity.createNewInstance(cursor);
          results.add(result);
  
          cursor.moveToNext();
      }
  
      db.close();
  
      return results;
  }
  </code></pre>
  
  <p>The crash with <strong>IllegalArgumentException: database not open</strong> happens on this line.</p>
  
  <pre><code>SQLiteDatabase db = defaultSQLiteOpenHelper.getWritableDatabase();
  </code></pre>
  
  <p>It happens exactly on the method (see below) <strong>createAllTables</strong>.</p>
  
  <p>The <a href="http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase%28%29" rel="nofollow">documentation</a> says:</p>
  
  <blockquote>
    <p>Create and/or open a database that will be used for reading and
    writing. The first time this is called, the database will be opened
    and onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int)
    and/or onOpen(SQLiteDatabase) will be called.</p>
  </blockquote>
  
  <p>In my opinion it should execute my DefaultSQLiteOpenhelper and create all database tables, but it doesn't do that:</p>
  
  <pre><code>public class DefaultSQLiteOpenHelper extends SQLiteOpenHelper implements ISQLiteOpenHelper
  {
      private static final String TAG = DefaultSQLiteOpenHelper.class.getSimpleName();
  
      private static final String DATABASE_NAME = "MY_DATABASE";
      private static final int DATABASE_VERSION = 1;
  
      @Inject
      public DefaultSQLiteOpenHelper(Context context)
      {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      //is called by the framework if the database doesn't exist
      @Override
      public void onCreate(SQLiteDatabase db)
      {
          Log.i(TAG, "no database found. Generating new database...");
  
          createAllTables(db);    
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
      {
           Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
  //       db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS); ///TODO when upgrade
  //       onCreate(db);
      }
  
      //The order must not be changed because sqlite doesn't allow
      //table modification. That means one cannot add constraints afterwards
      //which results in creating tables in a specific order
      private final void createAllTables(SQLiteDatabase db)
      {
          db.beginTransaction();
  
          //create lookups at first
          db.execSQL(TableFactory.createUsage());
          db.execSQL(TableFactory.createLanguage());
  
          db.execSQL(TableFactory.createAccount());
          db.execSQL(TableFactory.createPreferences());
          db.execSQL(TableFactory.createLanguageUsageRel());
          db.execSQL(TableFactory.createPantry());
          db.execSQL(TableFactory.createProduct());
  
          //populate lookups
          db.execSQL(DataFactory.populateUsage());
          db.execSQL(DataFactory.populateLanguage());
  
          db.setTransactionSuccessful();
          db.endTransaction();
          db.close();
      }
  }
  </code></pre>
  
  <p>The question is, why does it not open a database? Documentation says it has to. I don't accept any dirty workaround as solution like opening the database manually or something. What exactly is wrong on my code? I can't believe it's a bug in Android.</p>
  
  <p>Here is the stacktrace:</p>
  
  <blockquote>
    <p>05-27 17:25:50.370: I/DefaultSQLiteOpenHelper(719): no database found.
    Generating new database... 05-27 17:25:50.400: W/dalvikvm(719):
    threadid=10: thread exiting with uncaught exception (group=0x40015560)
    05-27 17:25:50.400: E/AndroidRuntime(719): FATAL EXCEPTION: AsyncTask</p>
    
    <h1>1 05-27 17:25:50.400: E/AndroidRuntime(719): java.lang.RuntimeException: An error occured while executing</h1>
    
    <p>doInBackground() 05-27 17:25:50.400: E/AndroidRuntime(719):   at
    android.os.AsyncTask$3.done(AsyncTask.java:200) 05-27 17:25:50.400:
    E/AndroidRuntime(719):    at
    java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    java.util.concurrent.FutureTask.run(FutureTask.java:138) 05-27
    17:25:50.400: E/AndroidRuntime(719):  at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    java.lang.Thread.run(Thread.java:1019) 05-27 17:25:50.400:
    E/AndroidRuntime(719): Caused by: java.lang.IllegalStateException:
    database not open 05-27 17:25:50.400: E/AndroidRuntime(719):  at
    android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:555)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:137)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    com.mydomain.android.base.persistence.PersistenceManager.loadAll(PersistenceManager.java:58)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    com.mydomain.android.base.main.StartupTask.isFirstStartAfterInstallation(StartupTask.java:107)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    com.mydomain.android.base.main.StartupTask.doInBackground(StartupTask.java:52)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    com.mydomain.android.base.main.StartupTask.doInBackground(StartupTask.java:18)
    05-27 17:25:50.400: E/AndroidRuntime(719):    at
    android.os.AsyncTask$2.call(AsyncTask.java:185) 05-27 17:25:50.400:
    E/AndroidRuntime(719):    at
    java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    05-27 17:25:50.400: E/AndroidRuntime(719):    ... 4 more</p>
  </blockquote>