복붙노트

[SQL] 안드로이드 여러 데이터베이스가 열립니다

SQL

안드로이드 여러 데이터베이스가 열립니다

나는 안드로이드를위한 IM 클라이언트를 만드는, 그리고 난 연락처 및 기타 정보의 저장을위한 데이터베이스 작업하고 ... 내 응용 프로그램에서 나는 활동 한 서비스가있다. 나는 서비스에와 활동에 모두 같은 시간에 세 개의 데이터베이스를 열어야합니다.

나는 데이터베이스 그들 쓰기의 동기화에 문제가없이보다 쉽게 ​​관리 할 수 ​​원하기 때문에 나는 세 개의 데이터베이스를 사용합니다. (이 분쇄 할 수있다 동 기적 때문에 내가 아는까지로 나는 데이터베이스에 작성해야합니다).

서비스에서 같은 시간에 활동에서 데이터베이스를 관리하기 위해, 나는 싱글 또는 DatabaseHelper의 정적 클래스가 나를 도울 수 있다고 생각 ...

나는 활동이 databasehelper 전역 개체를 만들어 시험을하기 시작했다 그래서 내가 마지막으로 연 데이터베이스 유적이 두 객체에서 열린 것으로 나타났습니다 프로젝트를 실행 한 후, 각자가하는 개의 다른 데이터베이스를 엽니 다 ((이유이다 사고?

내가이 일을 할 수있는 방법 누군가가 나에게 제안 할 수 있습니까? 감사합니다!

LE : 자세한 테스트 후, 나는 databasehelper의 정적 객체, 나는 활동에서 데이터베이스 개체를 가지고 같은 시간에 내가 서비스에 문 두, 활동 하나 하나를 만든있는 서비스를 열고 만든이 0에서 3000 실행 동일한 데이터베이스에 일부 값을 추가 한 후 데이터베이스를 읽습니다.

이 실행 한 후 나는 데이터베이스가 피트와 오류없이 실행 여전히 것으로 나타났습니다. 이상한 것은에 대한 서비스는 마감을위한 활동 일 이후에 실행되고 있다는 점이다. 왜 그런 겁니까? 감사합니다!

해결법

  1. ==============================

    1.나는 함께 열린 두 개의 데이터베이스를 포함하는 DatabaseAdapter 클래스가 있습니다.

    나는 함께 열린 두 개의 데이터베이스를 포함하는 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;
            }
        }
    }
    

    쉽게해야합니다 세 번째 하나를 추가 할 수 있습니다 :

  2. from https://stackoverflow.com/questions/4498664/android-multiple-databases-open by cc-by-sa and MIT license