title: SQLite database error when database is replaced
body: <p>I have and android application with an SQLite database. When I need to update my database I load all my information that I need to save to local variables, delete te installed database, copy the new one, and store the content of my variables back to the new database. My problem is that after copying the information back to the new database my applications crashes. But if I start my app again with the new database already copied the application starts normally with no problem and all my information is in the application. This is my log cat:</p>
  
  <pre><code>    05-17 15:11:16.563 4238-4652/? I/SendBroadcastPermission: action:android.net.wifi.RSSI_CHANGED, mPermissionType:0
  05-17 15:11:16.565 4238-4652/? E/WifiConfigStore: updateConfiguration freq=5180 BSSID=24:a2:e1:eb:18:c3 RSSI=-65 "InoveOnline_5Ghz"WPA_PSK
  05-17 15:11:16.823 4238-4652/? E/WifiConfigStore: updateConfiguration freq=5180 BSSID=24:a2:e1:eb:18:c3 RSSI=-65 "InoveOnline_5Ghz"WPA_PSK
  05-17 15:11:17.267 21634-21726/com.aquarismo I/FirebaseCrash: Sending crashes
  05-17 15:11:17.272 21634-21726/com.aquarismo E/SQLiteLog: (1032) 
  05-17 15:11:17.279 21634-21726/com.aquarismo E/UncaughtException: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (Sqlite code 1032), (OS error - 2:No such file or directory)
          at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
          at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:790)
          at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
          at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
          at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1507)
          at bbi.a(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):87)
          at bbz.run(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):30)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
          at java.lang.Thread.run(Thread.java:833)
  05-17 15:11:17.302 21634-21661/com.aquarismo I/DynamiteModule: Considering local module com.google.android.gms.tagmanager:9 and remote module com.google.android.gms.tagmanager:213
      Selected remote version of com.google.android.gms.tagmanager, version &gt;= 213
  05-17 15:11:17.519 21634-21725/com.aquarismo E/SQLiteLog: (1032) 
  05-17 15:11:17.521 21634-21725/com.aquarismo E/SQLiteDatabase: Error inserting ts=1526566277482 fatal=1 payload=[B@56ab04 size=1370
      android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (Sqlite code 1032), (OS error - 2:No such file or directory)
          at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
          at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:790)
          at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
          at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
          at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1507)
          at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1372)
          at bbx.run(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):45)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
          at java.lang.Thread.run(Thread.java:833)
  05-17 15:11:17.540 21634-21727/com.aquarismo I/FirebaseCrash: Sending crashes
  05-17 15:11:17.546 21634-21727/com.aquarismo E/SQLiteLog: (1032) 
  05-17 15:11:17.549 21634-21727/com.aquarismo E/UncaughtException: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (Sqlite code 1032), (OS error - 2:No such file or directory)
          at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
          at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:790)
          at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
          at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
          at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1507)
          at bbi.a(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):87)
          at bbz.run(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):30)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
          at java.lang.Thread.run(Thread.java:833)
  </code></pre>
  
  <p>My code to save my data and delete my old database:</p>
  
  <pre><code>String destDir = "/data/data/" + getPackageName() + "/databases/";
  String destPath = destDir + "AquaInfo37";
  File f = new File(destPath);
  
  // se nao existir cria DB
  if (!f.exists()) {
      File directory = new File(destDir);
      directory.mkdirs();
      // apaga todos os ficheiros (BD antigas) que possam existir na
      // diretoria se existir alguma bd, guarda os favoritos. Nome da Bd
      // antiga, se existir, esta em nome[0]
      if (directory.isDirectory()) {
          String[] array = directory.list();
          // guarda os favoritos da base de dados anterior
          for (int i = 0; i &lt; array.length; i++) {
              String nome = array[i];
              if (nome.contains("AquaInfo") &amp;&amp; nome.length() &lt; 11) {
                  // here I save my data to my variables saveRestoreDadosBaseDadosTemporario.getMeusDadosBaseDados(this, nome);
                  new_db = true;
                  break;
              }
          }
          // apaga todas as bd antigas que possam existir
          for (int i = 0; i &lt; array.length; i++) {
              new File(directory, array[i]).delete();
          }
      }
      // volta a criar a directoria
      directory.mkdirs();
      // copia para a BD para a nova directoria
      try {
          CopyDB(getBaseContext().getAssets().open("aquarismo"), new FileOutputStream(destPath));
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>