/**
* 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 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 {
if (version > mNewVersion) {
Log.wtf(TAG, "Can't downgrade read-only database from version " +
version + " to " + mNewVersion + ": " + db.getPath());
}
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.
*
* <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 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使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。
其中getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。
getReadableDatabase()方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。
分享到:
相关推荐
Android getReadableDatabase() 和 getWritableDatabase()分析对比 Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。(getReadableDatabase()方法中...
### Android中getWritableDatabase()与getReadableDatabase()的区别详解 在Android开发中,数据库操作是非常常见且重要的环节之一。为了高效地进行数据管理,Android提供了一系列API来帮助开发者处理数据库的相关...
SQLiteOpenHelper 是一个抽象类,就是说如果我们...不同的是,当数据库不可写入的时候(如磁盘空间已满)getReadableDatabase()方法返回的对象将以只读的方式去打开数据库,而 getWritableDatabase()方法则将出现异常。
SQLiteOpenHelper提供了两种方法来获取SQLiteDatabase对象:getReadableDatabase()和getWritableDatabase()。这两个方法都可以用来操作数据库,但是getReadableDatabase()方法只能读取数据库,而getWritableDatabase...
今天学习数据库的增删改查 调用 SQLiteOpenHelper的 getReadableDatabase()或 getWritableDatabase()方法都会返回一个SQLiteDatabase对象, 借助这个对象就可以对数据进行 CRUD 操作了。
SQLiteDatabase DB=getWritableDatabase(); ContentValues values=new ContentValues(); values.put("ItemName", ItemName); values.put("Money", Money); values.put("time", time); return DB.insert...
使用SharedPreferences示例2.SQLite数据库编程SQLiteOpenHelper类 常用方法自定义数据库子类(继承 SQLiteOpenHelper 类)创建数据库:getWritableDatabase()、getReadableDatabase()操作数据库(增、删、查、改...
SQLiteOpenHelper 提供了两个方法来获取 SQLiteDatabase 对象,getReadableDatabase() 和 getWritableDatabase(),这两个对象用于对数据进行操作。 Android 内置数据库 SQLite 是一种轻量级的关系数据库管理系统,...
另外本代码的SQLiteOepnHelper子类当中必须有该构造函数,必须通过super调用父类当中的构造函数,只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或...
数据库部分:在SQLiteOepnHelper的子类当中,必须有该构造函数,必须通过super调用父类当中的构造函数,只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建...
在实践中,通过调用SQLiteOpenHelper的getWritableDatabase()或getReadableDatabase()方法,可以创建数据库实例,同时触发onCreate()方法,从而执行数据库表的创建逻辑。当需要升级数据库时,只需提高数据库版本号,...
- 调用`copyDatabase()`方法在应用启动时复制数据库,然后通过`getWritableDatabase()`或`getReadableDatabase()`打开数据库进行读写操作。 3. **使用SQLiteOpenHelper的实例进行数据操作**: - 在你的应用中,你...
首先,调用`getWritableDatabase()`或`getReadableDatabase()`获取数据库实例,然后构造SQL的INSERT语句,最后执行插入操作。 ```java public long insertData(String tableName, ContentValues values) { ...
进行数据库的增删改查操作,我们需要实例化`SQLiteOpenHelper`的子类,并通过其`getWritableDatabase()`或`getReadableDatabase()`方法获取`SQLiteDatabase`对象。 1. 增(添加数据): ```java void insertData...
其次,SQLiteOpenHelper还提供了getWritableDatabase()和getReadableDatabase()两个方法,分别用于获取可写和只读的数据库实例。在日常开发中,大部分操作都会使用getWritableDatabase(),因为即使数据只读,Android...
接下来,我们可以使用`SQLiteOpenHelper`的`getWritableDatabase()`或`getReadableDatabase()`方法获取可读写或只读的数据库对象,然后执行SQL语句进行数据操作。例如,插入一条数据: ```java public void insert...
##### 1.6 关于`getWritableDatabase()` 和 `getReadableDatabase()` - `getWritableDatabase()`: 以可读写的方式创建/打开一个SQLite数据库并返回`SQLiteDatabase`对象。如果之前已经以可读写的方式打开过,并且...
读取数据通常通过SQLiteOpenHelper的子类的`getWritableDatabase()`或`getReadableDatabase()`方法获取数据库实例,然后使用`query()`或`rawQuery()`执行SQL查询。例如: ```java public List<DataModel> getAllData...