Code example for SQLiteDatabase

Methods: isOpenisReadOnly, close

0
     */ 
    public synchronized SQLiteDatabase getWritableDatabase() {
    	if (mHelper != null) {
    		return mHelper.getWritableDatabase();
    	} 
        if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
            return mDatabase;  // The database is already open for business
        } 
        boolean success = false;
        SQLiteDatabase db = null;
        try { 
            db = SQLiteDatabase.openOrCreateDatabase(mName, null);
 
            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();
                } 
            } 
            success = true;
            return db;
        } finally { 
            if (success) {
                if (mDatabase != null) {
                    try { mDatabase.close(); } catch (Exception e) { }
                } 
                mDatabase = db;
            } else { 
                if (db != null) db.close();
            }