2011-09-21 54 views
-1

我認爲這是一個相當容易的問題。我已經很年輕了。我想準備將使用數據庫的應用程序。在我展示的每個示例中,都有一個空的數據庫,其中應用程序首先啓動,然後有一些插入。我想有相當大的分貝的應用程序,所以我想在應用程序啓動時填充分貝。我如何準備數據庫並將其附加到程序中?不是空的LiteSQL DB在啓動

+0

請提問前搜索!您必須在資產文件夾中準備腳本!請搜索'-1' –

回答

2
  1. 把您填寫的數據庫中包的資產目錄,

  2. 在應用運行時間只需複製數據庫備份到 data/data/<package name>/database目錄應用程序的內部存儲。

  3. 然後使用它。

編輯:此爲複製數據庫從資產目錄數據庫目錄,

private void copyDataBase() throws IOException { 

     try { 
      // Open your local db as the input stream 
      InputStream myInput = myContext.getAssets().open("your Database file name"); 

      // Path to the just created empty db 
      String outFileName = "/data/data/<package name>/databases/"; 
      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(); 

     } catch (Exception e) { 

      Log.e("error", e.toString()); 

     } 

    }