Android: remove data from database -


i using this code (android-sqlite-asset-helper) load database file place in asset folder. works great.

however, processing of refreshing/upgrading database not simple, , wondering if there simple way manually remove data databases in app; in order load new database asset file.

you can use simple copy file override data in default database. overwriting default database new database file. following code work file, need little change here , there make work asset file. here method overwrite database file:

/**  * copies database file @ specified location on current  * internal application database.  **/  public boolean importdatabase(context context, string dbpath) throws ioexception {     file olddbfile = context.getapplicationcontext().getdatabasepath(dbschema.database_name);    // close sqliteopenhelper commit created empty    // database internal storage.    close();    file newdb = new file(dbpath);    if (newdb.exists()) {      fileutils.copyfile(new fileinputstream(newdb), new fileoutputstream(olddbfile));      // access copied database sqlitehelper cache , mark      // created.      getwritabledatabase().close();       return true;   }   return false; } 

fileutils class:

public class fileutils {   /**    * creates specified <code>tofile</code> byte byte copy of    * <code>fromfile</code>. if <code>tofile</code> exists,    * replaced copy of <code>fromfile</code>. name , path    * of <code>tofile</code> of <code>tofile</code>.<br/>    * <br/>    * <i> note: <code>fromfile</code> , <code>tofile</code> closed    * function.</i>    *    * @param fromfile - fileinputstream file copy from.    * @param tofile - fileinputstream file 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);     } {       try {         if (fromchannel != null) {           fromchannel.close();         }       } {         if (tochannel != null) {           tochannel.close();         }       }     }   } } 

i forget took copyfile method :(.

there 1 caveat: when user cleaning app data, database default.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -