Android getReadableDatabase() 和 getWritableDatabase()分析對比
Android getReadableDatabase() 和 getWritableDatabase()分析對比
Android使用getWritableDatabase()和getReadableDatabase()方法都可以獲取一個(gè)用于操作數(shù)據(jù)庫的SQLiteDatabase實(shí)例。(getReadableDatabase()方法中會(huì)調(diào)用getWritableDatabase()方法)
其中g(shù)etWritableDatabase() 方法以讀寫方式打開數(shù)據(jù)庫,一旦數(shù)據(jù)庫的磁盤空間滿了,數(shù)據(jù)庫就只能讀而不能寫,倘若使用的是getWritableDatabase() 方法就會(huì)出錯(cuò)。
getReadableDatabase()方法則是先以讀寫方式打開數(shù)據(jù)庫,如果數(shù)據(jù)庫的磁盤空間滿了,就會(huì)打開失敗,當(dāng)打開失敗后會(huì)繼續(xù)嘗試以只讀方式打開數(shù)據(jù)庫。如果該問題成功解決,則只讀數(shù)據(jù)庫對象就會(huì)關(guān)閉,然后返回一個(gè)可讀寫的數(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(); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 從零開始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
- Android基礎(chǔ)之Fragment與Activity交互詳解
- Android調(diào)用默認(rèn)瀏覽器打開指定Url的方法實(shí)例
- Android中使用Gson解析JSON數(shù)據(jù)的兩種方法
- Android中實(shí)現(xiàn)可滑動(dòng)的Tab的3種方式
- Android中判斷網(wǎng)絡(luò)連接是否可用及監(jiān)控網(wǎng)絡(luò)狀態(tài)
- Android開發(fā)之SQLite的使用方法
- android圖片壓縮的3種方法實(shí)例
相關(guān)文章
Android中ContentProvider和ContentResolver詳解
這篇文章主要介紹了Android中ContentProvider和ContentResolver詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁面效果
本文通過實(shí)例代碼較詳細(xì)的給大家介紹了Android?ViewPager?+?Fragment實(shí)現(xiàn)滑動(dòng)頁面效果,需要的朋友可以參考下2018-06-06Android實(shí)現(xiàn)自定義Crash handler記錄崩潰信息實(shí)例代碼
這篇文章主要給大家介紹了Android實(shí)現(xiàn)自定義Crash handler記錄崩潰信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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)判定技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android 實(shí)現(xiàn)秒轉(zhuǎn)換成時(shí)分秒的方法
這篇文章主要介紹了Android 實(shí)現(xiàn)秒轉(zhuǎn)換成時(shí)分秒的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Flutter實(shí)現(xiàn)用視頻背景的登錄頁的示例代碼
這篇文章主要介紹了Flutter實(shí)現(xiàn)用視頻背景的登錄頁的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Android自定義LocationMarker的實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹一個(gè)比較簡單的東西:自定義繪制Marker 其實(shí)就是自定義view, 跟軌跡沒太多關(guān)聯(lián),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(二)
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android中EditText禁止輸入表情的實(shí)例代碼
本篇文章主要介紹了Android中EditText禁止輸入表情的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Android 8.0不能自動(dòng)安裝APK問題的解決方法(完美適配)
這篇文章主要給大家介紹了關(guān)于Android 8.0不能自動(dòng)安裝APK問題的解決方法(完美適配),這里的自動(dòng)安裝是指下載完成后,自動(dòng)彈出安裝界面,而不是靜默安裝APK,文中介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07