java - Simple export and import of a SQLite database on Android -


i trying implement simple sqlite export/import backup purposes. export matter of storing copy of raw current.db file. want import delete old current.db file , rename imported.db file current.db. possible? when try solution, following error:

06-30 13:33:38.831: error/sqliteopenhelper(23570):     android.database.sqlite.sqlitedatabasecorruptexception: error code 11: database disk image malformed 

if @ raw database file in sqlite browser looks fine.

i use code in sqliteopenhelper in 1 of applications import database file.

edit: pasted fileutils.copyfile() method question.

sqliteopenhelper

public static string db_filepath = "/data/data/{package_name}/databases/database.db";  /**  * copies database file @ specified location on current  * internal application database.  * */ public boolean importdatabase(string dbpath) throws ioexception {      // close sqliteopenhelper commit created empty     // database internal storage.     close();     file newdb = new file(dbpath);     file olddb = new file(db_filepath);     if (newdb.exists()) {         fileutils.copyfile(new fileinputstream(newdb), new fileoutputstream(olddb));         // access copied database sqlitehelper cache , mark         // created.         getwritabledatabase().close();         return true;     }     return false; } 

fileutils

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();                 }             }         }     } } 

don't forget delete old database file if necessary.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -