0

I am using this code (android-sqlite-asset-helper) to load a database from a file place in the asset folder. This works great.

However, the processing of refreshing/upgrading the database is not simple, and I am wondering if there is a simple way to manually remove all data from the databases in the app; in order to load a new database from the asset file.

PatriceG
  • 3,851
  • 5
  • 28
  • 43
  • Reading skills 0/10 ... https://github.com/jgilfelt/android-sqlite-asset-helper#upgrades-via-overwrite – Selvin Jun 28 '16 at 11:24
  • @Selvin You are right, it seems you didn't read the question! ;-) – PatriceG Jun 28 '16 at 11:25
  • "remove all data from the databases in the app; in order to load a new database from the asset file." == force load – Selvin Jun 28 '16 at 11:26
  • This doesn't work, the old value of the database (for instance, the version number) are still in memory. Thank you anyway for you nice and constructive comments – PatriceG Jun 28 '16 at 11:41
  • *the old value of the database (for instance, the version number) are still in memory.* after updating the application? updating application will kill whole process, so it is not possible – Selvin Jun 28 '16 at 11:45
  • 1
    I think so, you should see this : http://stackoverflow.com/questions/6134103/clear-applications-data-programmatically – javad Jun 28 '16 at 12:10
  • @javadaskari Thanks! – PatriceG Jun 29 '16 at 12:04

2 Answers2

0

You can use simple copy file to override data in default database. Simply by overwriting default database with your new database file. The following code work only with a file, so you need a little change here and there to make it work with asset file. Here the method to overwrite the database file:

/**
 * Copies the database file at the specified location over the current
 * internal application database.
 **/
 public boolean importDatabase(Context context, String dbPath) throws IOException {

   File OldDbFile = context.getApplicationContext().getDatabasePath(DBSchema.DATABASE_NAME);
   // Close the SQLiteOpenHelper so it will commit the created empty
   // database to internal storage.
   close();
   File newDb = new File(dbPath);
   if (newDb.exists()) {
     FileUtils.copyFile(new FileInputStream(newDb), new FileOutputStream(OldDbFile));
     // Access the copied database so SQLiteHelper will cache it and mark
     // it as created.
     getWritableDatabase().close();

     return true;
  }
  return false;
}

FileUtils class:

public class FileUtils {
  /**
   * Creates the specified <code>toFile</code> as a byte for byte copy of the
   * <code>fromFile</code>. If <code>toFile</code> already exists, then it
   * will be replaced with a copy of <code>fromFile</code>. The name and path
   * of <code>toFile</code> will be that of <code>toFile</code>.<br/>
   * <br/>
   * <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
   * this function.</i>
   *
   * @param fromFile - FileInputStream for the file to copy from.
   * @param toFile - FileInputStream for the file to copy to.
   */
  public static 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);
    } finally {
      try {
        if (fromChannel != null) {
          fromChannel.close();
        }
      } finally {
        if (toChannel != null) {
          toChannel.close();
        }
      }
    }
  }
}

I really forget where I took the copyFile method :(.

There is one caveat: when user cleaning app data, the database will be back to default.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

It seems to work with this in the main activity creation:

getApplicationContext().deleteDatabase("mydatabase.db");
PatriceG
  • 3,851
  • 5
  • 28
  • 43