2017-05-14 6 views
-1

SQLiteデータベースをインポート/エクスポートしようとしていますが、エクスポートが機能しています。私はdatabase.dbを外部ストレージにエクスポートしていますが、インポートするとエラーは表示されませんが、インポートされたレコードは反映されません。SQLiteインポートが機能していません

ここに私のコードです。

public void importDatabase() throws IOException { 

    if(isExternalStorageGranted()){ 
     AlertDialog.Builder alert = new AlertDialog.Builder(this); 
     alert.setTitle("Import Database"); 
     alert.setMessage("do you want to replace database?"); 
     alert.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       try { 
        RemindersDbAdapter objRemindersDbAdapter = new RemindersDbAdapter(ImportExportActivity.this); 
        objRemindersDbAdapter.open(); 
        String oldDBPath = Environment.getExternalStorageDirectory().getPath() + separator + Constants.APP_FOLDER 
          + separator + SAMPLE_DB_NAME+".db"; 
        String currentDBPath = "/data/user/0/" + getPackageName() + "/databases/" + SAMPLE_DB_NAME; 

        // database to internal oldDBPath. 
        File newDb = new File(oldDBPath); 
        File oldDb = new File(currentDBPath); 

        copyFile(new FileInputStream(oldDb), new FileOutputStream(newDb)); 
        objRemindersDbAdapter.close(); 
       } catch (IOException e) { 
        Toast.makeText(ImportExportActivity.this, "Failed to import database", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 

     alert.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 
     alert.create().show(); 
    } 
}  

private void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException { 
    FileChannel fromChannel = null; 
    FileChannel toChannel = null; 
    try { 
     fromChannel = fromFile.getChannel(); 
     toChannel = toFile.getChannel(); 
     fromChannel.transferTo(0, fromChannel.size(), toChannel); 
     Toast.makeText(this, "Database Imported Successfully", Toast.LENGTH_SHORT).show(); 
    } finally { 
     try { 
      if (fromChannel != null) { 
       fromChannel.close(); 
      } 
     } finally { 
      if (toChannel != null) { 
       toChannel.close(); 
      } 
     } 
    } 
} 

private void exportDB() { 

    if(isExternalStorageGranted()){ 
     File sd = Environment.getExternalStorageDirectory(); 
     File data = Environment.getDataDirectory(); 
     FileChannel source = null; 
     FileChannel destination = null; 
     String currentDBPath = "/data/" + getApplicationContext().getPackageName() + "/databases/" + SAMPLE_DB_NAME; 
     String backupDBPath = SAMPLE_DB_NAME + ".db"; 
     File currentDB = new File(data, currentDBPath); 

     File directory = new File(Environment.getExternalStorageDirectory()+File.separator+ Constants.APP_FOLDER); 
     if(!directory.exists()) 
      directory.mkdirs(); 
     File backupDB = new File(directory, backupDBPath); 
     LogFile.appendLog("backupDB DB Path : -" + backupDB.getPath()); 

     if (backupDB.exists()) { 
      boolean isPrevfileDelete = backupDB.delete(); 
     } 
     try { 
      source = new FileInputStream(currentDB).getChannel(); 
      destination = new FileOutputStream(backupDB).getChannel(); 
      destination.transferFrom(source, 0, source.size()); 
      source.close(); 
      destination.close(); 
      Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

インポート後、インポートされたレコードはsqliteデータベースに反映されません。

+0

DB接続を正しく開いたり閉じたりしていない可能性がありますが、そのコードは表示されません。 –

+0

私はobjRemindersDbAdapter.open()を使用して開きます。 objRemindersDbAdapter.close();を閉じます。すでにコードがあります。 –

答えて

0

オープンしているデータベースファイルにはアクセスしないでください。

ファイル操作を行う前に、すべての接続が閉じていることを確認してください。

関連する問題