首页 > 试题广场 >

android中使用SQLiteOpenHelper这个辅助

[不定项选择题]
android中使用SQLiteOpenHelper这个辅助类时,可以生成一个数据库,并可以对数据库进行管理的方法可以是?
  • getWriteableDatabase()
  • getReadableDatabase()
  • getDatabase()
  • getAbleDatabase()
AB
发表于 2015-07-10 23:38:33 回复(0)
如果只是数据库的查询,则只需调用getReadableDatabase(),获取数据库的信息。
如果是修改的话,则使用 getWriteableDatabase()方法,对数据库进行修改。所以选A。
发表于 2015-02-28 21:51:07 回复(2)
getReadableDatabase()和 getWriteableDatabase()都可以得到一个可既可以读又可以写的数据库对象,不同的是,当磁盘空间满了之后, getReadableDatabase()得到的是一个只读的对象,而 getWriteableDatabase()则会抛出异常。
发表于 2015-09-18 13:49:46 回复(2)

Android使用 getWritableDatabase() 和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。(getReadableDatabase()方法中会调用getWritableDatabase()方法)

getReadableDatabase()并不是以只读方式打开数据库,而是先执行getWritableDatabase(),失败的情况下才以只读方式打开数据库.。

但getWritableDatabase()方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,

getWritableDatabase()打开数据库就会出错。getReadableDatabase()方法先以读写方式打开数据库,
倘若使用如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库.

/**
     * Create and/or open a database that will be used for reading and writing.
     * Once opened successfully, the database is ***d, 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 it.
     *
     * <p>Errors such as bad permissions or a full disk may cause this operation
     * to fail, but future attempts may succeed if the problem is fixed.</p>
     *
     * @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 {
                        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.
     *
     * @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();
        }
    }


发表于 2016-03-26 11:42:08 回复(5)
getReadableDatabase()并不是以只读方式打开数据库。getReadableDatabase()方法中会先调用getWritableDatabase()方法。一旦数据库的磁盘空间满了,数据库就只能读而不能写。这个时候getWritableDatabase()打开数据库就会抛异常。getReadableDatabase()方法虽然也会打开失败,但会尝试以只读方式打开数据库。
发表于 2019-02-25 08:57:56 回复(0)
getReadableDatabase()并不是获得只读数据库。而是先执行写操作,再执行读操作,当磁盘满了,就直接 获取只读数据库。而
getWriteableDatabase()获得直接读写方式数据库,当磁盘满了。直接抛出异常
发表于 2019-04-12 11:29:24 回复(0)
public SQLiteDatabase getWritableDatabase() { synchronized (this) { return getDatabaseLocked(true);
    }
}
public SQLiteDatabase getReadableDatabase() { synchronized (this) { return getDatabaseLocked(false);
    }
}
  • 源码中这两个方法句通过调用getDatabaseLocked获取数据库实例,不同的是参数一个为true一个为false。
    再来看下getReadableDatabase方法的注释说明。
    /**
       * 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.
       */
发表于 2019-02-28 16:46:36 回复(0)

Android使用 getWritableDatabase() 和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。(getReadableDatabase()方法中会调用getWritableDatabase()方法)

getReadableDatabase()并不是以只读方式打开数据库,而是先执行getWritableDatabase(),失败的情况下才以只读方式打开数据库.。

但getWritableDatabase()方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,

getWritableDatabase()打开数据库就会出错。getReadableDatabase()方法先以读写方式打开数据库,
倘若使用如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库
发表于 2017-10-09 16:37:04 回复(0)
getReadableDatabase()和 getWriteableDatabase()都可以得到一个可既可以读又可以写的数据库对象,不同的是,当磁盘空间满了之后, getReadableDatabase()得到的是一个只读的对象,而 getWriteableDatabase()则会抛出异常。
发表于 2017-03-06 07:51:04 回复(0)
对数据操作先读以判断数据库是否已满,如果未满正常则可进行读写操作,如果已满,则只读,reader中已经调用了write方法,
发表于 2016-08-28 00:08:29 回复(0)
getReadableDatabase()和   getWriteableDatabase()都可以得到一个可既可以读又可以写的数据库对象,不同的是,当磁盘空间满了之后,   getReadableDatabase()得到的是一个只读的对象,而   getWriteableDatabase()则会抛出异常
发表于 2016-06-20 16:32:31 回复(0)
A
发表于 2015-04-05 20:57:02 回复(0)
A
发表于 2015-03-26 14:38:59 回复(0)
选AB。最好用read的方法。
发表于 2015-03-08 22:07:00 回复(0)
AB
需要修改用
  • getWriteableDatabase()
不需要修改用两个都可以用。
发表于 2015-03-01 16:12:21 回复(0)