Android getReadableDatabase() 和 getWritableDatabase()分析對(duì)比
Android getReadableDatabase() 和 getWritableDatabase()分析對(duì)比
Android使用getWritableDatabase()和getReadableDatabase()方法都可以獲取一個(gè)用于操作數(shù)據(jù)庫(kù)的SQLiteDatabase實(shí)例。(getReadableDatabase()方法中會(huì)調(diào)用getWritableDatabase()方法)
其中g(shù)etWritableDatabase() 方法以讀寫(xiě)方式打開(kāi)數(shù)據(jù)庫(kù),一旦數(shù)據(jù)庫(kù)的磁盤(pán)空間滿了,數(shù)據(jù)庫(kù)就只能讀而不能寫(xiě),倘若使用的是getWritableDatabase() 方法就會(huì)出錯(cuò)。
getReadableDatabase()方法則是先以讀寫(xiě)方式打開(kāi)數(shù)據(jù)庫(kù),如果數(shù)據(jù)庫(kù)的磁盤(pán)空間滿了,就會(huì)打開(kāi)失敗,當(dāng)打開(kāi)失敗后會(huì)繼續(xù)嘗試以只讀方式打開(kāi)數(shù)據(jù)庫(kù)。如果該問(wèn)題成功解決,則只讀數(shù)據(jù)庫(kù)對(duì)象就會(huì)關(guān)閉,然后返回一個(gè)可讀寫(xiě)的數(shù)據(jù)庫(kù)對(duì)象。
源碼如下:
/**
* 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();
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 從零開(kāi)始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
- Android基礎(chǔ)之Fragment與Activity交互詳解
- Android調(diào)用默認(rèn)瀏覽器打開(kāi)指定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開(kāi)發(fā)之SQLite的使用方法
- android圖片壓縮的3種方法實(shí)例
相關(guān)文章
Android編程之Application設(shè)置全局變量及傳值用法實(shí)例分析
這篇文章主要介紹了Android編程之Application設(shè)置全局變量及傳值用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了全局變量及傳值的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
Android實(shí)現(xiàn)搜索保存歷史記錄功能
這篇文章主要介紹了Android實(shí)現(xiàn)搜索保存歷史記錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android的八種對(duì)話框的實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Android的八種對(duì)話框的實(shí)現(xiàn)代碼示例,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,有需要的小伙伴可以參考下。2017-09-09
Android程序結(jié)構(gòu)簡(jiǎn)單講解
在本篇文章里小編給大家分享一篇關(guān)于Android程序結(jié)構(gòu)的簡(jiǎn)單說(shuō)明內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。2019-02-02
Android 7.0系統(tǒng)webview 顯示https頁(yè)面空白處理方法
今天小編就為大家分享一篇Android 7.0系統(tǒng)webview 顯示https頁(yè)面空白處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機(jī)的步驟詳解
Android 模擬器一直以速度奇慢無(wú)比著稱,基本慢到不可用。本文介紹我一直在用的 Genymotion,速度不亞于真機(jī)。而且功能齊全,使用簡(jiǎn)單。下面這篇文章主要介紹了Ubuntu 14.04下創(chuàng)建Genymotion虛擬機(jī)的步驟,需要的朋友可以參考下。2017-03-03

