• getWritableDatabase()/getReadableDatabase()区别

    不要给名字误导
    服务器君一共花费 127.980 ms 进行了 3 次数据库查询,努力地为您提供了这个页面。
    广告很萌的

    在 Android 中使用 getWritableDatabase()getReadableDatabase() 方法都可以获取一个用于操作数据库的SQLiteDatabase实例。其中 getReadableDatabase() 方法中会调用 getWritableDatabase()方法。

    其中getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。

    getReadableDatabase() 方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。

    源码如下:

    /**
     * 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.
     *
     * 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.
     *
     * 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 SQLiteDatabase getWritableDatabase() {
    	synchronized (this) {
    		return getDatabaseLocked(true);
    	}
    }
    
    /**
     * 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.
     *
     * 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 SQLiteDatabase getReadableDatabase() {
    	synchronized (this) {
    		return getDatabaseLocked(false);
    	}
    }
    
    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();
    		}
    	}
    }
    
更多 推荐条目

Welcome to NowaMagic Academy!

现代魔法 推荐于 2013-02-27 10:23   

本章最新发布
随机专题
  1. [Python程序设计] Tornado源码解析 23 个条目
  2. [移动开发] Android SQLite增删查改实例(数据:魔弹之王) 2 个条目
  3. [移动开发] Layout_weight属性解析 5 个条目
  4. [JavaScript程序设计] 关于HTTP Keep-Alive 6 个条目
  5. [软件工程与项目管理] 浏览器的HTML解析器 8 个条目
  6. [数据结构] 散列表(哈希表) 13 个条目
  7. [软件工程与项目管理] 浏览器初步介绍 8 个条目
  8. [智力开发与知识管理] 超越整体性学习 5 个条目
  9. [软件工程与项目管理] 浏览器的CSS解析 7 个条目
  10. [数据库技术] MySQL常用自带函数 3 个条目
  11. [移动开发] Android Studio里的Gradle 3 个条目
  12. [Python程序设计] Django 入门知识浅介 10 个条目
窗口 -- [博客]