2016-04-01 6 views
-1

sqliteデータベースの更新を提供する関数を作成しています。私は最初にAndroid Appに入ったときにデータフォルダにコピーされたassetsフォルダにデータベース(demo.dbなど)があります。しかし、私は私のアプリのための新しいバージョンを持っているとき、私はすでにデータ/データfolder.Hereで作成された資産フォルダにsqliteのテーブルを交換する必要が私のコードです:アトリビュートからアセットのデータ/データにsqliteテーブルをコピーする

MySQLiteHelpterはSQLiteOpenHelperを拡張:

ここで
@Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     for(int i = oldVersion; i <= newVersion; i++){ 
      switch (i){ 
       case 8: 
        //delete the older table 
        db.execSQL("drop table if exists " + DFY_BD_DISTRICT); 

        //do something. 
        //I need to copy the table from the assets folder even I have already created the database in the previous version. 

        break; 
      } 
     } 
    } 

は私が初めてデータ/データフォルダに自分のデータベースをコピーするのに使用私のコードです:

public static void loadDB(Context context, String dbName) { 
    String packName = context.getPackageName(); 
    String dbPath = "data/data/" + packName + "/databases"; 

    try { 
     File e = new File(dbPath); 
     if(!e.exists()) { 
      e.mkdirs(); 
     } 

     File dest = new File(e, dbName); 
     if(dest.exists()) { 
      return; 
     } 

     dest.createNewFile(); 
     InputStream in = context.getResources().getAssets().open(dbName); 
     int size = in.available(); 
     byte[] buf = new byte[size]; 
     in.read(buf); 
     in.close(); 
     FileOutputStream out = new FileOutputStream(dest); 
     out.write(buf); 
     out.close(); 
    } catch (Exception var10) { 
     var10.printStackTrace(); 
    } 

} 

私は、インターネット上で検索しましたように、私は私に似たいくつかの問題を発見した:https://stackoverflow.com/questions/12264860/android-sqlite-database-copying-table-from-assets-to-local-app-databaseを 私はうれしいです)もしあなたが私に助けを与えることができます:) もっとコードや質問が必要な場合、私はできるだけ早く私のコードを貼り付けます。

答えて

0

次の機能を使用して、ローカルアセットデータベースをアンドロイドデバイスにコピーできます。

String DB_PATH = "/data/data/com.example.app/databases/" 


public String initialiseDB(String dbName,String copyas,Context context) 
    { 

     try 
     {    
      File f = new File(DB_PATH); 
      if (!f.exists()) 
      { 
       f.mkdir(); 
      } 

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

      // File for creating the database 
      String outFileName = DB_PATH + copyas; 

      File tmpFile = new File(outFileName); 


      // check if the database already exist 

      if (!tmpFile.exists()){ 
      //Log.d(LOG_TAG, outFileName + "database not available, initialising seed"); 
      OutputStream myOutput = new FileOutputStream(outFileName); 

      // transfer bytes from the input file to the output file 
      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) 
     { 
      e.printStackTrace(); 
      Log.e(LOG_TAG, e.toString()); 
      return "failure"; 
     } 

     return "SUCCESS"; 

    } 

私はこれがあなたを助けてくれることを願っています...達成するために何か問題がある場合は教えてください。

+0

ありがとうございます〜しかし、あなたのコードは、データ/データフォルダに資産をデータベースフォームのテーブルをコピーするのではなく、アプリケーションを初めて入力するデータベースをコピーするようです〜 – LinaInverce

+0

そのコードは通常動作しますAndroid 6.0以上? –

+0

あなたのケースでは、@ LinaInverce ----あなたは更新の古いdbデータを削除し、アセットからプログラムで新しいdbをコピーすることができます。 – beginner

関連する問題