getReadableDatabase() 和 getWritableDatabase()

来源:互联网 发布:无锡网络作协 编辑:程序博客网 时间:2024/06/04 18:29

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

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

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

源码如下:

[java] view plaincopy
  1. /** 
  2.      * Create and/or open a database that will be used for reading and writing. 
  3.      * Once opened successfully, the database is cached, so you can call this 
  4.      * method every time you need to write to the database.  Make sure to call 
  5.      * {@link #close} when you no longer need it. 
  6.      * 
  7.      * <p>Errors such as bad permissions or a full disk may cause this operation 
  8.      * to fail, but future attempts may succeed if the problem is fixed.</p> 
  9.      * 
  10.      * @throws SQLiteException if the database cannot be opened for writing 
  11.      * @return a read/write database object valid until {@link #close} is called 
  12.      */  
  13.     public synchronized SQLiteDatabase getWritableDatabase() {  
  14.         if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {  
  15.             return mDatabase;  // The database is already open for business  
  16.         }  
  17.   
  18.         if (mIsInitializing) {  
  19.             throw new IllegalStateException("getWritableDatabase called recursively");  
  20.         }  
  21.   
  22.         // If we have a read-only database open, someone could be using it  
  23.         // (though they shouldn't), which would cause a lock to be held on  
  24.         // the file, and our attempts to open the database read-write would  
  25.         // fail waiting for the file lock.  To prevent that, we acquire the  
  26.         // lock on the read-only database, which shuts out other users.  
  27.   
  28.         boolean success = false;  
  29.         SQLiteDatabase db = null;  
  30.         if (mDatabase != null) mDatabase.lock();  
  31.         try {  
  32.             mIsInitializing = true;  
  33.             if (mName == null) {  
  34.                 db = SQLiteDatabase.create(null);  
  35.             } else {  
  36.                 db = mContext.openOrCreateDatabase(mName, 0, mFactory);  
  37.             }  
  38.   
  39.             int version = db.getVersion();  
  40.             if (version != mNewVersion) {  
  41.                 db.beginTransaction();  
  42.                 try {  
  43.                     if (version == 0) {  
  44.                         onCreate(db);  
  45.                     } else {  
  46.                         onUpgrade(db, version, mNewVersion);  
  47.                     }  
  48.                     db.setVersion(mNewVersion);  
  49.                     db.setTransactionSuccessful();  
  50.                 } finally {  
  51.                     db.endTransaction();  
  52.                 }  
  53.             }  
  54.   
  55.             onOpen(db);  
  56.             success = true;  
  57.             return db;  
  58.         } finally {  
  59.             mIsInitializing = false;  
  60.             if (success) {  
  61.                 if (mDatabase != null) {  
  62.                     try { mDatabase.close(); } catch (Exception e) { }  
  63.                     mDatabase.unlock();  
  64.                 }  
  65.                 mDatabase = db;  
  66.             } else {  
  67.                 if (mDatabase != null) mDatabase.unlock();  
  68.                 if (db != null) db.close();  
  69.             }  
  70.         }  
  71.     }  
  72.   
  73.     /** 
  74.      * Create and/or open a database.  This will be the same object returned by 
  75.      * {@link #getWritableDatabase} unless some problem, such as a full disk, 
  76.      * requires the database to be opened read-only.  In that case, a read-only 
  77.      * database object will be returned.  If the problem is fixed, a future call 
  78.      * to {@link #getWritableDatabase} may succeed, in which case the read-only 
  79.      * database object will be closed and the read/write object will be returned 
  80.      * in the future. 
  81.      * 
  82.      * @throws SQLiteException if the database cannot be opened 
  83.      * @return a database object valid until {@link #getWritableDatabase} 
  84.      *     or {@link #close} is called. 
  85.      */  
  86.     public synchronized SQLiteDatabase getReadableDatabase() {  
  87.         if (mDatabase != null && mDatabase.isOpen()) {  
  88.             return mDatabase;  // The database is already open for business  
  89.         }  
  90.   
  91.         if (mIsInitializing) {  
  92.             throw new IllegalStateException("getReadableDatabase called recursively");  
  93.         }  
  94.   
  95.         try {  
  96.             return getWritableDatabase();  
  97.         } catch (SQLiteException e) {  
  98.             if (mName == nullthrow e;  // Can't open a temp database read-only!  
  99.             Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);  
  100.         }  
  101.   
  102.         SQLiteDatabase db = null;  
  103.         try {  
  104.             mIsInitializing = true;  
  105.             String path = mContext.getDatabasePath(mName).getPath();  
  106.             db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);  
  107.             if (db.getVersion() != mNewVersion) {  
  108.                 throw new SQLiteException("Can't upgrade read-only database from version " +  
  109.                         db.getVersion() + " to " + mNewVersion + ": " + path);  
  110.             }  
  111.   
  112.             onOpen(db);  
  113.             Log.w(TAG, "Opened " + mName + " in read-only mode");  
  114.             mDatabase = db;  
  115.             return mDatabase;  
  116.         } finally {  
  117.             mIsInitializing = false;  
  118.             if (db != null && db != mDatabase) db.close();  
  119.         }  
  120.     } 
http://blog.csdn.net/alex_zhuang/article/details/7342840