Android multiple databases open
Asked Answered
S

1

13

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).

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...

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!

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!

Spile answered 21/12, 2010 at 11:19 Comment(1)
Perhaps, you worked with the database from the main thread in the activity and in the service.Sanctitude
C
25

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 :)

Chatterer answered 21/12, 2010 at 13:36 Comment(3)
Thank you for you answer, I will try your databaseHelper too.Spile
I don't think it's goin to be possible but, could you open both databases at the same time?Rebba
@JuanJoséMeleroGómez sure it is. You just need to call open() with internal and external parameter. As stated in the first sentence: This class was used to deal with two databases at the same time keeping both open.Chatterer

© 2022 - 2024 — McMap. All rights reserved.