コード例 #1
0
  /**
   * Create and/or open a database. This will be the same object returned by {@link
   * #getWritableDatabase} unless some problem, such as a full disk, requires the database to be
   * opened read-only. In that case, a read-only database object will be returned. If the problem is
   * fixed, a future call to {@link #getWritableDatabase} may succeed, in which case the read-only
   * database object will be closed and the read/write object will be returned in the future.
   *
   * <p class="caution">Like {@link #getWritableDatabase}, this method may take a long time to
   * return, so you should not call it from the application main thread, including from {@link
   * android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
   *
   * @throws SQLiteException if the database cannot be opened
   * @return a database object valid until {@link #getWritableDatabase} or {@link #close} is called.
   */
  public synchronized SQLiteDatabase getReadableDatabase() {
    if (mDatabase != null) {
      if (!mDatabase.isOpen()) {
        // darn! the user closed the database by calling mDatabase.close()
        mDatabase = null;
      } else {
        return mDatabase; // The database is already open for business
      }
    }

    if (mIsInitializing) {
      throw new IllegalStateException("getReadableDatabase called recursively");
    }

    try {
      return getWritableDatabase();
    } catch (SQLiteException e) {
      if (mName == null) throw e; // Can't open a temp database read-only!
      Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
    }

    SQLiteDatabase db = null;
    try {
      mIsInitializing = true;
      String path = mContext.getDatabasePath(mName).getPath();
      db =
          SQLiteDatabase.openDatabase(
              path, mPasswd, mFactory, SQLiteDatabase.OPEN_READONLY, mErrorHandler);
      if (db.getVersion() != mNewVersion) {
        throw new SQLiteException(
            "Can't upgrade read-only database from version "
                + db.getVersion()
                + " to "
                + mNewVersion
                + ": "
                + path);
      }

      onOpen(db);
      Log.w(TAG, "Opened " + mName + " in read-only mode");
      mDatabase = db;
      return mDatabase;
    } finally {
      mIsInitializing = false;
      if (db != null && db != mDatabase) db.close();
    }
  }
コード例 #2
0
  /**
   * 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 {@link #onCreate}, {@link #onUpgrade} and/or {@link
   * #onOpen} will be called.
   *
   * <p>Once opened successfully, the database is cached, so you can call this method every time you
   * need to write to the database. (Make sure to call {@link #close} when you no longer need the
   * database.) Errors such as bad permissions or a full disk may cause this method to fail, but
   * future attempts may succeed if the problem is fixed.
   *
   * <p class="caution">Database upgrade may take a long time, you should not call this method from
   * the application main thread, including from {@link android.content.ContentProvider#onCreate
   * ContentProvider.onCreate()}.
   *
   * @throws SQLiteException if the database cannot be opened for writing
   * @return a read/write database object valid until {@link #close} is called
   */
  public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null) {
      if (!mDatabase.isOpen()) {
        // darn! the user closed the database by calling mDatabase.close()
        mDatabase = null;
      } else if (!mDatabase.isReadOnly()) {
        return mDatabase; // The database is already open for business
      }
    }

    if (mIsInitializing) {
      throw new IllegalStateException("getWritableDatabase called recursively");
    }

    // If we have a read-only database open, someone could be using it
    // (though they shouldn't), which would cause a lock to be held on
    // the file, and our attempts to open the database read-write would
    // fail waiting for the file lock.  To prevent that, we acquire the
    // lock on the read-only database, which shuts out other users.

    boolean success = false;
    SQLiteDatabase db = null;
    if (mDatabase != null) mDatabase.lock();
    try {
      mIsInitializing = true;
      if (mName == null) {
        db = SQLiteDatabase.create(null);
      } else {
        db =
            SQLiteDatabase.openDatabase(
                mName, mPasswd, mFactory,
                0); // mContext.openOrCreateDatabase(mName, 0, mFactory, mErrorHandler);
      }

      int version = db.getVersion();
      if (version != mNewVersion) {
        db.beginTransaction();
        try {
          if (version == 0) {
            onCreate(db);
          } else {
            if (version > mNewVersion) {
              onDowngrade(db, version, mNewVersion);
            } else {
              onUpgrade(db, version, mNewVersion);
            }
          }
          db.setVersion(mNewVersion);
          db.setTransactionSuccessful();
        } finally {
          db.endTransaction();
        }
      }

      onOpen(db);
      success = true;
      return db;
    } finally {
      mIsInitializing = false;
      if (success) {
        if (mDatabase != null) {
          try {
            mDatabase.close();
          } catch (Exception e) {
          }
          mDatabase.unlock();
        }
        mDatabase = db;
      } else {
        if (mDatabase != null) mDatabase.unlock();
        if (db != null) db.close();
      }
    }
  }
コード例 #3
0
  private SQLiteDatabase getDatabaseLocked(boolean writable) {
    if (mDatabase != null) {
      if (!mDatabase.isOpen()) {
        // Darn!  The user closed the database by calling mDatabase.close().
        mDatabase = null;
      } else if (!writable || !mDatabase.isReadOnly()) {
        // The database is already open for business.
        return mDatabase;
      }
    }

    if (mIsInitializing) {
      throw new IllegalStateException("getDatabase called recursively");
    }

    SQLiteDatabase db = mDatabase;
    try {
      mIsInitializing = true;

      if (db != null) {
        if (writable && db.isReadOnly()) {
          db.reopenReadWrite();
        }
      } else if (mName == null) {
        db = SQLiteDatabase.create(null);
      } else {
        try {
          if (DEBUG_STRICT_READONLY && !writable) {
            final String path = mContext.getDatabasePath(mName).getPath();
            db =
                SQLiteDatabase.openDatabase(
                    path, mFactory, SQLiteDatabase.OPEN_READONLY, mErrorHandler);
          } else {
            db =
                mContext.openOrCreateDatabase(
                    mName,
                    mEnableWriteAheadLogging ? Context.MODE_ENABLE_WRITE_AHEAD_LOGGING : 0,
                    mFactory,
                    mErrorHandler);
          }
        } catch (SQLiteException ex) {
          if (writable) {
            throw ex;
          }
          Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", ex);
          final String path = mContext.getDatabasePath(mName).getPath();
          db =
              SQLiteDatabase.openDatabase(
                  path, mFactory, SQLiteDatabase.OPEN_READONLY, mErrorHandler);
        }
      }

      onConfigure(db);

      final int version = db.getVersion();
      if (version != mNewVersion) {
        if (db.isReadOnly()) {
          throw new SQLiteException(
              "Can't upgrade read-only database from version "
                  + db.getVersion()
                  + " to "
                  + mNewVersion
                  + ": "
                  + mName);
        }

        db.beginTransaction();
        try {
          if (version == 0) {
            onCreate(db);
          } else {
            if (version > mNewVersion) {
              onDowngrade(db, version, mNewVersion);
            } else {
              onUpgrade(db, version, mNewVersion);
            }
          }
          db.setVersion(mNewVersion);
          db.setTransactionSuccessful();
        } finally {
          db.endTransaction();
        }
      }

      onOpen(db);

      if (db.isReadOnly()) {
        Log.w(TAG, "Opened " + mName + " in read-only mode");
      }

      mDatabase = db;
      return db;
    } finally {
      mIsInitializing = false;
      if (db != null && db != mDatabase) {
        db.close();
      }
    }
  }