title: App crash upon reverting to backup database
body: <p>I'm making an Android app that downloads a database from a server to use locally on the phone. After a successful download, I make a copy of the .db file and save it in the data/data/app_name/files folder. In the event that the database becomes corrupted, I delete the original .db file and make a new one in it's place, writing everything from the backup file over to the new .db file. After this happens though, the next time that I try and use the database, I get an error saying that the file was deleted and that I'm attempting to write to a read-only database. What I would like to know is if this is a good method of backing up my database, and if so, what is going wrong with reverting to the backup.</p>
  
  <p>Crash log</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.troubleshooting, PID: 10421
      android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
      #################################################################
      Error Code : 1032 (SQLITE_READONLY_DBMOVED)
      Caused By : Database or Journal file have been removed.
          (attempt to write a readonly database (code 1032))
      #################################################################
          at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
          at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:904)
          at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
          at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
          at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2111)
          at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2039)
          at Model.GuideDatabase.createTable(GuideDatabase.java:102)
          at Model.GuideDatabase.writeToDatabase(GuideDatabase.java:238)
          at Model.GuideDatabase.writeAllFiles(GuideDatabase.java:248)
          at Controller.DownloadGuideTask.onPostExecute(DownloadGuideTask.java:153)
          at Controller.DownloadGuideTask.onPostExecute(DownloadGuideTask.java:24)
          at android.os.AsyncTask.finish(AsyncTask.java:695)
          at android.os.AsyncTask.-wrap1(Unknown Source:0)
          at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
          at android.os.Handler.dispatchMessage(Handler.java:105)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6944)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
  </code></pre>
  
  <p>My makeBackup method</p>
  
  <pre><code>public void makeBackup()
      {
          File file = new File(DATABASE_PATH);
          String backupName = curContext.getFilesDir() + BACKUP_NAME;
  
          try {
              FileInputStream in = new FileInputStream(file);
              OutputStream out = new FileOutputStream(backupName);
  
              byte[] buffer = new byte[1024];
              int length;
  
              while((length = in.read(buffer)) &gt; 0)
              {
                  out.write(buffer, 0, length);
              }
              in.close();
              out.flush();
              out.close();
              Log.i("Backup", "Backup made successfully at " + backupName);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  </code></pre>
  
  <p>My revertToBackup method</p>
  
  <pre><code>public boolean revertToBackup()
      {
          boolean success = false;
  
          //setup
          File file = new File(DATABASE_PATH);
          String backupName = curContext.getFilesDir() + BACKUP_NAME;
  
          try {
  
              //get rid of the old database file, it may be corrupted or something like that
              if(file.exists())
                  file.delete();
              file = new File(backupName);
              if(file.exists()) {
  
                  file.setReadable(true);
                  file.setWritable(true);
  
  
                  //take in from the backup file, put into the new file
                  FileInputStream in = new FileInputStream(backupName);
                  OutputStream out = new FileOutputStream(DATABASE_PATH);
  
                  byte[] buffer = new byte[1024];
                  int length;
  
                  //write the whole file to the new file
                  while ((length = in.read(buffer)) &gt; 0) {
                      out.write(buffer, 0, length);
                  }
  
                  //cleanup
                  in.close();
                  out.flush();
                  out.close();
                  Log.i("Backup", "Backup successfully restored");
                  success = true;
              }
              else {
                  Log.i("Backup", "Backup not found");
                  Toast t = Toast.makeText(curContext, "Backup not found, must re-download database to continue", Toast.LENGTH_LONG);
                  t.show();
              }
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          return success;
      }
  </code></pre>