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

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

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

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

Android使用getWritableDatabase()和getReadableDatabase()方法都可以獲取一個用于操作數(shù)據(jù)庫的SQLiteDatabase實例。(getReadableDatabase()方法中會調用getWritableDatabase()方法)

其中getWritableDatabase() 方法以讀寫方式打開數(shù)據(jù)庫,一旦數(shù)據(jù)庫的磁盤空間滿了,數(shù)據(jù)庫就只能讀而不能寫,倘若使用的是getWritableDatabase() 方法就會出錯。

getReadableDatabase()方法則是先以讀寫方式打開數(shù)據(jù)庫,如果數(shù)據(jù)庫的磁盤空間滿了,就會打開失敗,當打開失敗后會繼續(xù)嘗試以只讀方式打開數(shù)據(jù)庫。如果該問題成功解決,則只讀數(shù)據(jù)庫對象就會關閉,然后返回一個可讀寫的數(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(); 
    } 
  } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

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

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

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

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

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

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

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

    Android程序結構簡單講解

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

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

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

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

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

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

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

    Android 調用百度地圖API示例

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

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

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

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

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

最新評論

武山县| 宁波市| 闽侯县| 潜山县| 菏泽市| 揭西县| 子洲县| 平顺县| 阿拉尔市| 济宁市| 岳阳县| 昌宁县| 蒲城县| 天水市| 襄樊市| 玉山县| 汾西县| 自治县| 峨山| 博客| 天祝| 隆德县| 邻水| 永兴县| 新干县| 汉阴县| 东乌珠穆沁旗| 响水县| 大关县| 宁城县| 普格县| 昔阳县| 婺源县| 涟源市| 革吉县| 河南省| 浦县| 郯城县| 元朗区| 雷州市| 平果县|