Android getReadableDatabase() 和 getWritableDatabase()分析對比
Android getReadableDatabase() 和 getWritableDatabase()分析對比
Android使用getWritableDatabase()和getReadableDatabase()方法都可以獲取一個用于操作數(shù)據(jù)庫的SQLiteDatabase實例。(getReadableDatabase()方法中會調(diào)用getWritableDatabase()方法)
其中g(shù)etWritableDatabase() 方法以讀寫方式打開數(shù)據(jù)庫,一旦數(shù)據(jù)庫的磁盤空間滿了,數(shù)據(jù)庫就只能讀而不能寫,倘若使用的是getWritableDatabase() 方法就會出錯。
getReadableDatabase()方法則是先以讀寫方式打開數(shù)據(jù)庫,如果數(shù)據(jù)庫的磁盤空間滿了,就會打開失敗,當(dāng)打開失敗后會繼續(xù)嘗試以只讀方式打開數(shù)據(jù)庫。如果該問題成功解決,則只讀數(shù)據(jù)庫對象就會關(guān)閉,然后返回一個可讀寫的數(shù)據(jù)庫對象。
源碼如下:
/** * Create and/or open a database that will be used for reading and writing. * 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 it. * * <p>Errors such as bad permissions or a full disk may cause this operation * to fail, but future attempts may succeed if the problem is fixed.</p> * * @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; if (mDatabase != null) mDatabase.lock(); try { mIsInitializing = true; if (mName == null) { db = SQLiteDatabase.create(null); } else { db = mContext.openOrCreateDatabase(mName, 0, 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) { } mDatabase.unlock(); } mDatabase = db; } else { if (mDatabase != null) mDatabase.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"); } 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, mFactory, SQLiteDatabase.OPEN_READONLY); 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(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android中ContentProvider和ContentResolver詳解
這篇文章主要介紹了Android中ContentProvider和ContentResolver詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Android?ViewPager?+?Fragment實現(xiàn)滑動頁面效果
本文通過實例代碼較詳細(xì)的給大家介紹了Android?ViewPager?+?Fragment實現(xiàn)滑動頁面效果,需要的朋友可以參考下2018-06-06Android實現(xiàn)自定義Crash handler記錄崩潰信息實例代碼
這篇文章主要給大家介紹了Android實現(xiàn)自定義Crash handler記錄崩潰信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02Android判斷設(shè)備網(wǎng)絡(luò)連接狀態(tài)及判斷連接方式的方法
這篇文章主要介紹了Android判斷設(shè)備網(wǎng)絡(luò)連接狀態(tài)及判斷連接方式的方法,涉及Android針對網(wǎng)絡(luò)連接的相關(guān)判定技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法
這篇文章主要介紹了Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Android自定義LocationMarker的實現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹一個比較簡單的東西:自定義繪制Marker 其實就是自定義view, 跟軌跡沒太多關(guān)聯(lián),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法(二)
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android 8.0不能自動安裝APK問題的解決方法(完美適配)
這篇文章主要給大家介紹了關(guān)于Android 8.0不能自動安裝APK問題的解決方法(完美適配),這里的自動安裝是指下載完成后,自動彈出安裝界面,而不是靜默安裝APK,文中介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07