title: Copy a sqlite table from assets to data/data in Android
body: <p>I am making a function that provides sqlite database update. I have a database(such as demo.db) in assets folder that is copied to my data folder when I enter the Android App for the first time. However, when I have a new version for my app, I need to replace a sqlite table in the assets folder which has already been created in the data/data folder.Here is my code:</p>
  
  <p>MySQLiteHelpter extends SQLiteOpenHelper:</p>
  
  <pre><code>@Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          for(int i = oldVersion; i &lt;=  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;
              }
          }
      }
  </code></pre>
  
  <p>Here is my code I use to copy my database to data/data folder for the first time:</p>
  
  <pre><code>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();
      }
  
  }
  </code></pre>
  
  <p>As I have searched on the Internet,I have found some question similar to mine:<a href="https://stackoverflow.com/questions/12264860/android-sqlite-database-copying-table-from-assets-to-local-app-database">https://stackoverflow.com/questions/12264860/android-sqlite-database-copying-table-from-assets-to-local-app-database</a>
  I am glad that if you can give me some help   : )
  If you need more code or more question,I will paste my code as soon as possible.</p>