sqlite - Updating prepopulated database in Android -


i lost last 3 hours trying this. im making app ship db filled populated tables not change on clients devices. managed putting in assets folder , copying stream of bytes appropriate data folder on phone. problem when wish update database, cant work. delete db in data folder on phone (from code), copying new database fails, or copied db has appropriate size no tables in it. how this? or there simpler way? can open db directly assets (it simplest way if can, cant find how access path assets code)?

here code using:

public class databasehelper extends sqliteopenhelper {      private static final string db_path = "/data/data/com.project.mydb/databases/";     private static final string db_name = "mydb.db";     private static final string db_table = "words";     private static final int db_version = 6;     private static final string tag = "databasehelper";     int id = 0;     random random = new random();     private sqlitedatabase mydatabase;     private final context mycontext;      public databasehelper(context context){         super(context, db_name, null, db_version);         this.mycontext = context;     }      @override     public void oncreate(sqlitedatabase db){         createdb();     }      @override     public void onupgrade (sqlitedatabase db, int oldversion, int newversion){         log.w(tag, "upgrading db version " + oldversion + " " +                 newversion + ", destroy old data");         db.execsql("drop table if exists " + db_table);         oncreate(db);     }      public void createdatabase(){         createdb();     }      private void createdb(){         boolean dbexist = dbexists();         if(!dbexist){             copydatabase();         }         else if(dbexist){             copydatabase();         }     }      private boolean dbexists(){         sqlitedatabase db = null;         try{             string dbpath = db_path + db_name;             db = sqlitedatabase.opendatabase(dbpath, null, sqlitedatabase.open_readwrite);             db.setlocale(locale.getdefault());             db.setlockingenabled(true);             db.setversion(db_version);         }         catch(sqliteexception e){             log.e("sql helper", "database not found");         }         if(db != null){             db.close();         }         return db != null ? true : false;     }      private void copydatabase(){         inputstream istream = null;         outputstream ostream = null;         string outfilepath = db_path + db_name;         try{             istream = mycontext.getassets().open(db_name);             ostream = new fileoutputstream(outfilepath);             byte[] buffer = new byte[1024];             int length;             while((length = istream.read(buffer))>0){                 ostream.write(buffer,0,length);             }             ostream.flush();             ostream.close();             istream.close();         }         catch(ioexception ioe){             throw new error("problem copying database resource file.");         }     }      public void opendatabase() throws sqlexception {         string mypath = db_path + db_name;         mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite);     }      @override     public synchronized void close(){         if (mydatabase != null)             mydatabase.close();         super.close();     } } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -