最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android getReadableDatabase() 和 getWritableDatabase()分析對(duì)比

 更新時(shí)間:2017年06月08日 14:44:03   投稿:lqh  
這篇文章主要介紹了Android getReadableDatabase() 和 getWritableDatabase()分析對(duì)比的相關(guān)資料,需要的朋友可以參考下

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ì)本站的支持!

相關(guān)文章

  • Android編程之Application設(shè)置全局變量及傳值用法實(shí)例分析

    Android編程之Application設(shè)置全局變量及傳值用法實(shí)例分析

    這篇文章主要介紹了Android編程之Application設(shè)置全局變量及傳值用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了全局變量及傳值的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-12-12
  • Android實(shí)現(xiàn)搜索保存歷史記錄功能

    Android實(shí)現(xiàn)搜索保存歷史記錄功能

    這篇文章主要介紹了Android實(shí)現(xiàn)搜索保存歷史記錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android的八種對(duì)話框的實(shí)現(xiàn)代碼示例

    Android的八種對(duì)話框的實(shí)現(xiàn)代碼示例

    本篇文章主要介紹了Android的八種對(duì)話框的實(shí)現(xiàn)代碼示例,這里整理了詳細(xì)的代碼,非常具有實(shí)用價(jià)值,有需要的小伙伴可以參考下。
    2017-09-09
  • Android程序結(jié)構(gòu)簡(jiǎn)單講解

    Android程序結(jié)構(gòu)簡(jiǎn)單講解

    在本篇文章里小編給大家分享一篇關(guān)于Android程序結(jié)構(gòu)的簡(jiǎn)單說(shuō)明內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。
    2019-02-02
  • Android?studio下載安裝使用SVN的方法

    Android?studio下載安裝使用SVN的方法

    在AndroidStudio中開(kāi)發(fā)版本控制,除了Git就是SVN,和Eclipse不同,Android Studio沒(méi)有提供單獨(dú)的插件,只能和SVN客戶端關(guān)聯(lián)使用,這篇文章主要介紹了Android?studio使用SVN的方法,需要的朋友可以參考下
    2022-09-09
  • Android 環(huán)境變量的配置方法

    Android 環(huán)境變量的配置方法

    本文主要介紹Android 環(huán)境變量的配置方法,這里詳細(xì)講解了如何實(shí)現(xiàn)環(huán)境變量的配置方法,有興趣的小伙伴可以參考下
    2016-09-09
  • Android 7.0系統(tǒng)webview 顯示https頁(yè)面空白處理方法

    Android 7.0系統(tǒng)webview 顯示https頁(yè)面空白處理方法

    今天小編就為大家分享一篇Android 7.0系統(tǒng)webview 顯示https頁(yè)面空白處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Android 調(diào)用百度地圖API示例

    Android 調(diào)用百度地圖API示例

    在Android開(kāi)發(fā)中有一個(gè)非常重要的應(yīng)用就是實(shí)時(shí)定位,通過(guò)手機(jī)在手機(jī)地圖上進(jìn)行實(shí)時(shí)定位,定位當(dāng)前手機(jī)的位置,這篇文章主要介紹了Android 調(diào)用百度地圖API示例,有興趣的可以了解一下。
    2017-01-01
  • Android Shader著色器/渲染器的用法解析

    Android Shader著色器/渲染器的用法解析

    這篇文章主要介紹了Android Shader著色器/渲染器的用法解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機(jī)的步驟詳解

    Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機(jī)的步驟詳解

    Android 模擬器一直以速度奇慢無(wú)比著稱,基本慢到不可用。本文介紹我一直在用的 Genymotion,速度不亞于真機(jī)。而且功能齊全,使用簡(jiǎn)單。下面這篇文章主要介紹了Ubuntu 14.04下創(chuàng)建Genymotion虛擬機(jī)的步驟,需要的朋友可以參考下。
    2017-03-03

最新評(píng)論

乐昌市| 静乐县| 紫阳县| 韶山市| 辽宁省| 衡南县| 新津县| 汕头市| 新蔡县| 齐河县| 渝中区| 南木林县| 墨江| 新竹县| 思南县| 盐亭县| 彭泽县| 长治市| 皋兰县| 长垣县| 宜章县| 新宁县| 江阴市| 德令哈市| 泰和县| 互助| 榕江县| 广丰县| 柏乡县| 泸溪县| 中超| 华坪县| 乐清市| 特克斯县| 柘城县| 城固县| 建湖县| 澄江县| 汾阳市| 禹州市| 龙陵县|