2010-12-21 87 views
11

我正在爲Android的IM客戶端,我正在與數據庫一起存儲聯繫人和其他信息...在我的應用程序中,我有一個活動和一個服務。我需要在服務和活動上同時打開三個數據庫。Android多個數據庫打開

我使用三個數據庫,因爲我希望數據庫管理更容易,而不會有寫入同步的問題。 (據我所知,我需要同步寫入數據庫,因爲它可能會粉碎)。

要管理從服務,並在同一時間活動的數據庫,我想一個單身或靜態類DatabaseHelper的可以幫我...

所以我已經開始通過進行測試使得在活動二databasehelper全局對象,每一個打開diferent數據庫,正在運行的項目後,我已經注意到,最後打開的數據庫保持在兩個對象開:?((,爲什麼會出現這種情況

有人可以sugest我如何使這項工作? 謝謝!

L .E .:經過更多測試後,我創建了一個數據庫幫助器的Static對象,打開一個服務,從該服務中我從活動中獲取數據庫對象,同時我爲語句創建了兩個,一個在活動中,一個在服務中,從0到3000,並添加一些值到同一個數據庫,然後它讀取數據庫。

在這次運行後,我注意到數據庫仍然在腳下,沒有錯誤地運行。奇怪的是,服務僅在完成工作後的活動之後運行。這是爲什麼? 謝謝!

+0

也許,你與數據庫從活動主線程和服務工作。 – 2017-09-08 06:22:30

回答

24

我有一個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; 
     } 
    } 
} 

添加第三個應該很容易:)

+0

謝謝你的回答,我也會試試你的databaseHelper。 – Cata 2010-12-22 06:00:47

+0

我不認爲它是可能的,但是,你可以同時打開兩個數據庫嗎? – 2015-12-16 18:35:01

+1

@JuanJoséMeleroGómez確定。你只需要用內部和外部參數調用open()。正如第一句話所述:這個類同時用於處理兩個數據庫,同時保持兩個數據庫的打開狀態。 – WarrenFaith 2015-12-17 01:42:20