首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解析方法'reopenReadWrite()‘

无法解析方法'reopenReadWrite()‘
EN

Stack Overflow用户
提问于 2017-10-12 02:27:27
回答 1查看 505关注 0票数 1

我正在尝试创建一个登录页面,并使用SQLiteOpenHelper类连接到SQLite数据库。但是SQLiteOpenHelper.java中的默认代码在db.reopenReadWrite()行显示错误;我检查了SQLiteDatabase.java文件是否有reopenReadWrite()方法;您能帮我解决这个问题吗

EN

回答 1

Stack Overflow用户

发布于 2017-10-12 02:37:06

正如您在the source code中看到的那样,此方法通过@hide javadoc attribute隐藏

代码语言:javascript
复制
/**
 * 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()

代码语言:javascript
复制
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();
            }

        //.......................
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46695162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档