Code example for SQLiteDatabase

Methods: isOpenisReadOnly, close

0
     * 
     * @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 && mDatabase.isOpen() && !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;
        boolean doUnlock = false;
        if (mDatabase != null) {
        	doUnlock = true;
        	mLock.lock();
        } 
        try { 
            mIsInitializing = true;
            if (mDbFilePath == null) {
                db = SQLiteDatabase.create(null);
            } else { 
                db = SQLiteDatabase.openOrCreateDatabase(mDbFilePath, mFactory);
            } 
 
            int version = db.getVersion();
            if (version != mNewVersion) {
                db.beginTransaction();
                try { 
                    if (version == 0) {
                        onCreate(db);
                    } 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) { }
                } 
                if (doUnlock)
	                mLock.unlock();
                mDatabase = db;
            } else { 
            	if (doUnlock)
	                mLock.unlock();
                if (db != null) db.close();
            } 
        } 
    } 
 
    /** 
     * 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. 
     * 
     * @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 && mDatabase.isOpen()) {
            return mDatabase;  // The database is already open for business
        } 
 
        if (mIsInitializing) {
            throw new IllegalStateException("getReadableDatabase called recursively");