无法解析方法'reopenReadWrite()'

问题描述:

我想创建一个登录页面,并使用SQLiteOpenHelper类连接到SQLite数据库。但SQLiteOpenHelper.java中的默认代码在db.reopenReadWrite()行显示错误。 我检查了SQLiteDatabase.java文件的方法reopenReadWrite(); u能帮助我解决这个无法解析方法'reopenReadWrite()'

+1

PLS。分享你的代码。 –

这种方法隐藏与@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的一部分,你不能使用该方法,无需使用反射。

最好的方法是使用公共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(); 
      } 

     //....................... 
+0

谢谢你帮助我! –