Android的多个数据库开放 [英] Android multiple databases open

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

问题描述

我想提出一个IM客户端,Android和我正在与用于存储联系人等信息的数据库...在我的应用程序,我有一个活动,一个服务。我需要在同一时间上的服务和活动打开三个数据库两者。

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全局对象在活动进行测试,每一个打开diferent数据库,运行该项目后,我已经注意到,最近打开的数据库保持在两个对象打开:((,为什么会出现这种情况?

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?

有人可以sugest我怎样才能使这项工作? 谢谢!

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

LE:经过测试,我做了databasehelper的静态对象,打开从中我把数据库对象的活动,并在同一时间,我做了两个for语句,一个在活动,一个在服务,从运行服务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天全站免登陆