title: Android: SQLite database helper runs onCreate every time I open my app
body: <p>I'm using an extension of the SQLiteOpenHelper. My understanding is that onCreate only runs when the requested database does not exist. onOpen should run every time a database is opened.</p>
  
  <p>This seems to work between activities within my app - I need to instantiate a new instance of the database helper in each activity, and onOpen is run but not onCreate.  However each time I restart the app, onCreate is being called.  Am I misunderstanding how this should work or did I simply implement it wrong?</p>
  
  <p>Here's my helper class:</p>
  
  <pre><code>public class DBWrapper extends SQLiteOpenHelper {
      private static final String DATABASE_NAME = "MyTest.db";
      private static final int DATABASE_VERSION = 2;
  
      public DBWrapper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          Log.d("SQLite DBWrapper","OnCreate run");
          ver1(db);
          ver2(db);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          Log.d("SQLite DBWrapper","OnUpgrade run; oldVer: " + oldVersion + "; newVer: " + newVersion);
          if (oldVersion &lt; 2) {ver2(db);}
      }
  
      @Override
      public void onOpen(SQLiteDatabase db) {
          Log.d("SQLite DBWrapper","OnOpen run");
      }
  
      private void ver1(SQLiteDatabase db) {
          Log.d("SQLite Wrapper","Creating new Version 1");
          db.execSQL("create table UserList (ID integer primary key, Name String, State String, Email String, Status String);");
          db.execSQL("create table Contacts (ContactID integer, Display_Name String, Email_Address String, IsPrimary integer);");
      }
  
      private void ver2 (SQLiteDatabase db) {
          Log.d("SQLite Wrapper","Updating to Version 2");
          db.execSQL("create table ActivityRecord (UserID String, ActID String, ActDate date, Rating REAL, Comment String, RateDate date, primary key(UserID,ActID));");
      }
  
  //Other methods for inserting and retrieving data
  }
  </code></pre>
  
  <p>This is how I create an instance of the helper in each activity:</p>
  
  <pre><code>private DBWrapper DBHelper;
  private SQLiteDatabase db;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_test);
      DBHelper = new DBWrapper(this);
      db = DBHelper.getWritableDatabase();
  }
  </code></pre>
  
  <p>My logcat is indicating every time I restart my app (in debug mode at least) the onCreate is being called (along with ver1 and ver2).  What have I done wrong?</p>
  
  <p>UPDATE: I added more logging and found that A) This is happening on both release and debug variants; B) when I logged db.getVersion() in onCreate, it reported "0". So that either means it forgot the version number when I closed and reopened the app or the db was deleted when the app closed.</p>