Android多数据库打开 [英] Android multiple databases open

查看:34
本文介绍了Android多数据库打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a IM client for android and I am working with databases for storing the contacts and other info's... In my app I have an activity and one service. I need to open three databases in the same time both on the service and on the activity.

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

I use three database because I want the databases to be managed more easily without having problems with the synchronization of writing in them. (as far as I know I need to write in the database synchronously because it may crush).

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

To manage the databases from the service and from the activity in the same time, I thought that a singleton or a static class of DatabaseHelper could help me...

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

So I have started to make a test by making two databasehelper global objects in the activity, each one opens a diferent database, after running the project i've noticed that the last opened database remains opened in both objects :((, why is this happening?

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

Can somebody sugest me how can i make this work? Thank you!

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

L.E.: after more tests, I made a Static object of databasehelper, open a service from which I take the database object from activity and in the same time i made two for statements, one in activity and one in service that run from 0 to 3000 and adds some values into the same database and then it reads the database.

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

After this run i noticed that the database is still in feet and running without errors. The strange thing is that the service for is running only after the activity for finishes the job. Why is that? Thank you!

推荐答案

我有一个 DatabaseAdapter 类,它包含两个一起打开的数据库.

I have a DatabaseAdapter class which contains two databases which are opened together.

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;
        }
    }
}

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

to add a third one should be easy :)

这篇关于Android多数据库打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆