【问题标题】:Android multiple databases openAndroid多个数据库打开
【发布时间】:2011-05-28 18:33:14
【问题描述】:

我正在为 android 制作一个 IM 客户端,我正在使用用于存储联系人和其他信息的数据库...在我的应用程序中,我有一个活动和一项服务。我需要在服务和活动上同时打开三个数据库。

我使用三个数据库,因为我希望数据库更易于管理,而不会出现写入同步的问题。 (据我所知,我需要同步写入数据库,因为它可能会崩溃)。

为了同时从服务和活动管理数据库,我认为单例或静态类的 DatabaseHelper 可以帮助我...

所以我开始通过在活动中创建两个 databasehelper 全局对象来进行测试,每个都打开一个不同的数据库,运行项目后我注意到最后打开的数据库在两个对象中仍然打开:((,为什么会这样?

有人可以建议我如何使这项工作吗? 谢谢!

LE:经过更多测试后,我创建了一个数据库助手的静态对象,打开一个服务,我从该服务中获取数据库对象,同时我创建了两个 for 语句,一个在活动中,一个在服务中运行0 到 3000 并将一些值添加到同一个数据库中,然后读取数据库。

在这次运行之后,我注意到数据库仍在正常运行并且没有错误。奇怪的是,服务仅在活动完成后才运行。这是为什么? 谢谢!

【问题讨论】:

  • 也许,您在活动和服务的主线程中使用数据库。

标签: sql android database sqlite


【解决方案1】:

我有一个 DatabaseAdapter 类,其中包含两个一起打开的数据库。

public class DatabaseAdapter {
    /** Identifier for the internal database */
    public static final int             INTERNAL            = 0;
    /** Identifier for the external database */
    public static final int             EXTERNAL                = 1;

    private final SQLiteOpenHelper[]    mDatabaseManager    = new SQLiteOpenHelper[2];
    private final SQLiteDatabase[]      mDatabases          = new SQLiteDatabase[2];

    /**
     * Constructs the database and open it.
     */
    public DatabaseAdapter() {
        // Open the internal_db
        mDatabaseManager[INTERNAL] = new InternalDatabaseManager(MyApplication.getInstance());
        mDatabases[INTERNAL] = mDatabaseManager[INTERNAL].getWritableDatabase();
    }

    /**
     * Checks the database state and throws an {@link IllegalStateException} if database isn't open.
     * Should always be used before starting to access the database.
     * 
     * @param type Type of the database. Can be INTERNAL or EXTERNAL.
     */
    public void checkDbState(int type) {
        if (mDatabases[type] == null || !mDatabases[type].isOpen()) {
            throw new IllegalStateException("The database has not been opened");
        }
    }

    /**
     * Closes the database of the given type.
     * 
     * @param type Type of the database. Can be INTERNAL or EXTERNAL.
     */
    public void close(int type) {
        if (mDatabases[type].isOpen()) {
            mDatabases[type].close();
            mDatabases[type] = null;
            if (mDatabaseManager[type] != null) {
                mDatabaseManager[type].close();
                mDatabaseManager[type] = null;
            }
        }
    }

    /**
     * @param type Type of the database. Can be INTERNAL or EXTERNAL.
     * @return true if the database is open, false otherwise.
     */
    public boolean isOpen(int type) {
        return mDatabases[type] != null && mDatabases[type].isOpen();
    }

    /**
     * Opens the default database.
     * 
     * @param type Type of the database. Can be INTERNAL or EXTERNAL.
     */
    public void open(int type) {
        switch (type) {
            case INTERNAL:
                mDatabaseManager[INTERNAL] = new InternalDatabaseManager(MyApplication.getInstance());
                if (!isOpen(INTERNAL)) {
                    mDatabases[INTERNAL] = mDatabaseManager[INTERNAL].getWritableDatabase();
                }
            break;
            case EXTERNAL:
                mDatabaseManager[EXTERNAL] = new ExternalDatabaseManager(MyApplication.getInstance(), Constants.EXTERNAL_DB_PATH, 1);
                if (!isOpen(EXTERNAL)) {
                    mDatabases[EXTERNAL] = mDatabaseManager[EXTERNAL].getWritableDatabase();
                }
            break;
        }
    }
}

添加第三个应该很容易:)

【讨论】:

  • 谢谢你的回答,我也试试你的databaseHelper。
  • 我不认为这是可能的,但是你能同时打开两个数据库吗?
  • @JuanJoséMeleroGómez 确定是的。您只需要使用内部和外部参数调用 open() 即可。如第一句话所述:该类用于同时处理两个数据库,同时保持两个数据库打开。
猜你喜欢
  • 1970-01-01
  • 2012-03-23
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-08
相关资源
最近更新 更多