我正在尝试创建一个登录页面并使用SQLiteOpenHelper类连接到SQLite数据库。但是SQLiteOpenHelper.java中的默认代码在db.reopenReadWrite()行显示错误; 我检查过SQLiteDatabase.java文件的方法是reopenReadWrite(); 你可以帮我解决这个问题吗?
答案 0 :(得分:1)
您可以在@hide javadoc attribute中看到the source code隐藏此方法:
/**
* Reopens the database in read-write mode.
* If the database is already read-write, does nothing.
*
* @throws SQLiteException if the database could not be reopened as requested, in which
* case it remains open in read only mode.
* @throws IllegalStateException if the database is not open.
*
* @see #isReadOnly()
* @hide
*/
public void reopenReadWrite() {
synchronized (mLock) {
throwIfNotOpenLocked();
if (!isReadOnlyLocked()) {
return; // nothing to do
}
// Reopen the database in read-write mode.
final int oldOpenFlags = mConfigurationLocked.openFlags;
mConfigurationLocked.openFlags = (mConfigurationLocked.openFlags & ~OPEN_READ_MASK)
| OPEN_READWRITE;
try {
mConnectionPoolLocked.reconfigure(mConfigurationLocked);
} catch (RuntimeException ex) {
mConfigurationLocked.openFlags = oldOpenFlags;
throw ex;
}
}
}
由于它不是公共API的一部分,因此您无法在不使用Reflection的情况下使用该方法。
最好的方法是使用公共API listed in the SQLiteDatabase documentation中的方法。
您需要的只是SQLiteOpenHelper#getReadableDatabase()
方法,该方法会调用getDatabaseLocked()
,内部调用db.reopenReadWrite()
:
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();
}
//.......................