SQLiteOpenHelper中的getWritableDatabase和getReadableDatabase會的區別
1. 兩個方法幹嘛的?
兩個方法都是用於獲取資料庫的讀寫物件
,並不是字面上一個獲取讀取資料庫的物件,另一個獲取寫資料庫的物件。
2. getWritableDatabase()
原始碼註釋:
/**
* 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.
*
* <p>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.</p>
*
* <p class="caution">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 synchronized SQLiteDatabase getWritableDatabase();
- 它會呼叫並返回一個可以讀寫資料庫的物件
- 在第一次呼叫時會呼叫onCreate的方法
- 當資料庫存在時會呼叫onOpen方法
- 結束時呼叫onClose方法
3. getReadableDatabase()
原始碼註釋:
/**
* 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.
*
* <p class="caution">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.
*/
- 它會呼叫並返回一個可以讀寫資料庫的物件
- 在第一次呼叫時會呼叫onCreate的方法
- 當資料庫存在時會呼叫onOpen方法
- 結束時呼叫onClose方法
4. 區別
是不是上面兩個總結一樣?
然後事實呢?
- 兩個方法都是返回讀寫資料庫的物件,但是當磁碟已經滿了時,
getWritableDatabase
會拋異常,而getReadableDatabase
不會報錯,它此時不會返回讀寫資料庫的物件,而是僅僅返回一個讀資料庫的物件。 -
getReadableDatabase
會在問題修復後繼續返回一個讀寫的資料庫物件。 - 兩者都是資料庫操作,可能存在延遲等待,所以儘量不要在主執行緒中呼叫。
相關文章
- PHP 中的 -> 和 :: 的區別PHP
- mysql中!=和is not的區別MySql
- JavaScript中for in 和for of的區別JavaScript
- mysql中“ ‘ “和 “ ` “的區別MySql
- Js中for in 和for of的區別JS
- JavaScript中==和===的區別JavaScript
- Mybatis中#{}和${}傳參的區別及#和$的區別小結MyBatis
- JS中的!=、== 、!==、=== 的用法和區別JS
- java中的==和equal的區別Java
- JS中的!=、== 、!==、===的用法和區別。JS
- java 中equals和==的區別Java
- SQL中where和on的區別SQL
- Python 中 is 和 == 的區別Python
- Python中is和==的區別Python
- Java中equals和==的區別Java
- oracle中in和exists的區別Oracle
- Linux中“>”和“>>”的區別Linux
- vue2.0中的:is和is的區別Vue
- http中的get和post的區別HTTP
- js中!和!!的區別與用法JS
- javascrit中undefined和null的區別JavaUndefinedNull
- swift中Class和Struct的區別SwiftStruct
- js中AMD和CMD的區別JS
- php中TCP和UDP的區別PHPTCPUDP
- Java中Vector和ArrayList的區別Java
- oracle中distinct和group by的區別Oracle
- deferred中done和then的區別
- jquery中prop和attr的區別jQuery
- Jquery中attr和prop的區別jQuery
- Mysql 中 MyISAM 和 InnoDB 的區別MySql
- 程式中fork和vfork的區別
- js中null和undefined的區別JSNullUndefined
- js中undefined和null的區別JSUndefinedNull
- MyBatis中#{}和${}的區別詳解MyBatis
- Java中 equals() 方法和 == 的區別Java
- Jquery中.attr()和.data()的區別jQuery
- ruby中的== eql?和equal?區別
- JS中class和id的區別JS