Example #1
0
  /**
   * Opens the database, and upgrades it if necessary.
   *
   * @param context the Context to use for opening the database
   * @param databaseFile Name of the file to be initialized.
   */
  private void initDatabase(Context context, String databaseFile) {
    try {
      mDatabase = context.openOrCreateDatabase(databaseFile, 0, null);
    } catch (SQLiteException e) {
      // try again by deleting the old db and create a new one
      if (context.deleteDatabase(databaseFile)) {
        mDatabase = context.openOrCreateDatabase(databaseFile, 0, null);
      }
    }

    if (mDatabase == null) {
      // Not much we can do to recover at this point
      Log.e(LOGTAG, "Unable to open or create " + databaseFile);
      return;
    }

    if (mDatabase.getVersion() != DATABASE_VERSION) {
      mDatabase.beginTransactionNonExclusive();
      try {
        createTable();
        mDatabase.setTransactionSuccessful();
      } finally {
        mDatabase.endTransaction();
      }
    }
  }