Android getReadableDatabase() 和 getWritableDatabase()
来源:互联网 发布:大数据对零售业的影响 编辑:程序博客网 时间:2024/05/30 05:40
来自http://www.cnblogs.com/Free-Thinker/p/4331999.html
Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。(getReadableDatabase()方法中会调用getWritableDatabase()方法)
其中getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。
getReadableDatabase()方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。
自己补充 Android SDK 21代码
/** * 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 SQLiteDatabase getWritableDatabase() { synchronized (this) { return getDatabaseLocked(true); } } /** * 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. */ public SQLiteDatabase getReadableDatabase() { synchronized (this) { return getDatabaseLocked(false); } } private SQLiteDatabase getDatabaseLocked(boolean writable) { if (mDatabase != null) { if (!mDatabase.isOpen()) { // Darn! The user closed the database by calling mDatabase.close(). mDatabase = null; } else if (!writable || !mDatabase.isReadOnly()) { // The database is already open for business. return mDatabase; } } if (mIsInitializing) { throw new IllegalStateException("getDatabase called recursively"); } SQLiteDatabase db = mDatabase; try { mIsInitializing = true; if (db != null) { if (writable && db.isReadOnly()) { db.reopenReadWrite(); } } else if (mName == null) { db = SQLiteDatabase.create(null); } else { try { if (DEBUG_STRICT_READONLY && !writable) { final String path = mContext.getDatabasePath(mName).getPath(); db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY, mErrorHandler); } else { db = mContext.openOrCreateDatabase(mName, mEnableWriteAheadLogging ? Context.MODE_ENABLE_WRITE_AHEAD_LOGGING : 0, mFactory, mErrorHandler); } } catch (SQLiteException ex) { if (writable) { throw ex; } Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", ex); final String path = mContext.getDatabasePath(mName).getPath(); db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY, mErrorHandler); } } onConfigure(db); final int version = db.getVersion(); if (version != mNewVersion) { if (db.isReadOnly()) { throw new SQLiteException("Can't upgrade read-only database from version " + db.getVersion() + " to " + mNewVersion + ": " + mName); } db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { if (version > mNewVersion) { onDowngrade(db, version, mNewVersion); } else { onUpgrade(db, version, mNewVersion); } } db.setVersion(mNewVersion); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } onOpen(db); if (db.isReadOnly()) { Log.w(TAG, "Opened " + mName + " in read-only mode"); } mDatabase = db; return db; } finally { mIsInitializing = false; if (db != null && db != mDatabase) { db.close(); } } }
0 0
- Android getReadableDatabase() 和 getWritableDatabase()
- Android getReadableDatabase() 和 getWritableDatabase()
- Android getWritableDatabase()和getReadableDatabase区别
- Android getWritableDatabase()和getReadableDatabase区别
- Android使用getWritableDatabase()和getReadableDatabase()方法
- Android中getWritableDatabase()和getReadableDatabase()方法区别
- Android Sqlite 之 getWritableDatabase()和getReadableDatabase()区别
- Android数据库(1)--getReadableDatabase() 和 getWritableDatabase()
- Android-getWritableDatabase()与getReadableDatabase()
- getReadableDatabase() 和 getWritableDatabase()
- getReadableDatabase() 和 getWritableDatabase()
- getReadableDatabase() 和 getWritableDatabase()
- getReadableDatabase() 和 getWritableDatabase()
- android SQLite getWritableDatabase VS getReadableDatabase
- getWritableDatabase()和getReadableDatabase()的区别
- getWritableDatabase()和getReadableDatabase()的区别
- getWritableDatabase()和getReadableDatabase()方法区别
- getWritableDatabase()和getReadableDatabase()方法区别
- 再学习---Android相对布局(RelativeLayout)
- 唯爱小粽子:Mac下Nginx启动、重启和关闭
- 活动模块UIWebView
- JSONArray的应用
- awakeFromNib
- Android getReadableDatabase() 和 getWritableDatabase()
- iOS 利用UISCrollView作广告自动轮播效果
- cordova创建iOS项目
- 设计模式之中介者模式
- PHP获取某年第几周的开始日期和结束日期
- 李白打酒(DFS搜索)
- Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
- Java学习篇之---Eclipse中运行python程序
- 数据库性能优化之sql优化