2011-08-17 19 views
1

我需要在單個.apk的運行時綁定.db和.apk,以便用戶可以下載並運行該應用程序。我不喜歡。數據庫在資產,原始,URL路徑,因爲我們需要在運行時更改.db和我們有這麼多的用戶和不同的用戶,我們有不同的.db。它是基於ERP的應用程序。在此先感謝如何在一個.apk中綁定.apk和.db,以便用戶可以下載並運行應用程序

+0

我不確定你在問什麼,你是否還說你無法在運行時下載或創建數據庫?你到底什麼意思綁定 –

+0

嗨感謝評論actualy我們需要綁定到.db到.db所以產生後我們能夠給.apk給用戶,他可以使用。對於不同的用戶,我們需要相同的.apk和不同的數據庫。 –

回答

1

您將.db放入您的/ raw /目錄中。

然後在運行時將其複製到位於應用程序內存中的新數據庫中,該數據庫隨後可以修改。

//By calling this method and empty database will be created into the default system path 
    //of your application so we are gonna be able to overwrite that database with our database. 
    this.getReadableDatabase(); 

    /** 
    * Copies your database from your local assets-folder to the just created empty database in the 
    * system folder, from where it can be accessed and handled. 
    * This is done by transfering bytestream. 
    * */ 
    private void copyDataBase() throws IOException{ 

     //Open your local db as the input stream 
     InputStream myInput = myContext.getAssets().open(DB_NAME); 

     // Path to the just created empty db 
     String outFileName = DB_PATH + DB_NAME; 

     //Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName); 

     //transfer bytes from the inputfile to the outputfile 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 

     //Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 

    } 

的代碼是不是我的,是從全教程中使出在:Using your own DB in Android

如果你希望把不同的數據庫文件在/生/你可能想現在的使用多APK的市場允許它或製作一個ant腳本來在APK構建時選擇不同的數據庫。

相關問題