当前位置: 首页 > 编程笔记 >

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

韦寒
2023-03-14
本文向大家介绍Android getReadableDatabase() 和 getWritableDatabase()分析对比,包括了Android getReadableDatabase() 和 getWritableDatabase()分析对比的使用技巧和注意事项,需要的朋友参考一下

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

Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。(getReadableDatabase()方法中会调用getWritableDatabase()方法)

其中getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。

getReadableDatabase()方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。

源码如下:

/** 
   * 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(); 
    } 
  } 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

 类似资料:
  • 我想把一些数据存储到sqlite数据库中,但是在我的DBHandler和DBGen类中发生了错误,我无法找到这个错误,我想存储用户名,密码,确认密码,手机号码和电子邮件ID。 启动活性 下面是LogCat特定的问题代码 12-31 12:09:17.749:E/AndroidRuntime(962):java.lang.nullPointerException 12-31 12:09:17.749

  • 本文向大家介绍Jquery对象和Dom对象的区别分析,包括了Jquery对象和Dom对象的区别分析的使用技巧和注意事项,需要的朋友参考一下 在讨论之前,先约定好定义变量的风格。 如果获取的对象是jQuery对象,那么在变量前加上$,例如: 如果获取的是DOM对象,则定义如下:   jQuery对象不能使用DOM中的方法,但是如果对jQuery对象所提供的方法不熟悉,或者jQuery没有封装想要的方

  • 本文向大家介绍浅谈MySQL和Lucene索引的对比分析,包括了浅谈MySQL和Lucene索引的对比分析的使用技巧和注意事项,需要的朋友参考一下 MySQL和Lucene都可以对数据构建索引并通过索引查询数据,一个是关系型数据库,一个是构建搜索引擎(Solr、ElasticSearch)的核心类库。两者的索引(index)有什么区别呢?以前写过一篇《Solr与MySQL查询性能对比》,只是简单的

  • 本文向大家介绍对比分析MySQL语句中的IN 和Exists,包括了对比分析MySQL语句中的IN 和Exists的使用技巧和注意事项,需要的朋友参考一下 背景介绍 最近在写SQL语句时,对选择IN 还是Exists 犹豫不决,于是把两种方法的SQL都写出来对比一下执行效率,发现IN的查询效率比Exists高了很多,于是想当然的认为IN的效率比Exists好,但本着寻根究底的原则,我想知道这个结论

  • 本文向大家介绍JAVA反射机制中getClass和class对比分析,包括了JAVA反射机制中getClass和class对比分析的使用技巧和注意事项,需要的朋友参考一下 java有两个获得类名的方法getClass()和class(),这两个方法看似一样,实则不然。这两个方法涉及到了java中的反射。 所谓反射,可以理解为在运行时期获取对象类型信息的操作。传统的编程方法要求程序员在编译阶段决定使

  • 本文向大家介绍Java泛型类型通配符和C#对比分析,包括了Java泛型类型通配符和C#对比分析的使用技巧和注意事项,需要的朋友参考一下 c#的泛型没有类型通配符,原因是.net的泛型是CLR支持的泛型,而Java的JVM并不支持泛型,只是语法糖,在编译器编译的时候都转换成object类型 类型通配符在java中表示的是泛型类型的父类 编译上面的程序,test(strList) 处将发生编译错误,意