sqlite - Can't open database from assets folder [Android] -
sorry such lame problem, can't solve 4 hours. i'm trying copy database /assets folder use sqliteopenhelper, when try open inputstream gives me error:
e/trace: error opening trace file: no such file or directory (2) code:
public static final string database_path = "/data/data/com.mycomp.myapp/databases/"; public static final string database_name = "database.db"; @override protected void oncreate(bundle savedinstancestate) { try { copydatabase(getapplicationcontext()); } catch (ioexception e) { e.printstacktrace(); log.d(tag, "blad" + e.tostring()); } } private void copydatabase(context context) throws ioexception { string outfilename = database_path + database_name; inputstream myinput = context.getassets().open("database.db"); outputstream myoutput = new fileoutputstream(outfilename); byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } myoutput.flush(); myoutput.close(); myinput.close(); }
can try code , please tell me result. sure check file names.
@override protected void oncreate(bundle savedinstancestate) { //....... databasehelper dbhelper = new databasehelper(this); dbhelper.createdatabase(); dbhelper.opendatabase(); // stuff cursor data =dbhelper.sample_use_of_helper(); dbhelper.close(); } class databasehelper extends sqliteopenhelper { private static string db_path = "/data/data/com.mycomp.myapp/databases/"; private static string db_name = "database.db"; private sqlitedatabase mydatabase; private final context mycontext; public databasehelper (context context) { super(context, db_name, null, 1); this.mycontext = context; } public void cratedatabase() throws ioexception { boolean vtvarmi = isdatabaseexist(); if (!vtvarmi) { this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } private void copydatabase() throws ioexception { // open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; // open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); // transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } // close streams myoutput.flush(); myoutput.close(); myinput.close(); } private boolean isdatabaseexist() { sqlitedatabase kontrol = null; try { string mypath = db_path + db_name; kontrol = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } catch (sqliteexception e) { kontrol = null; } if (kontrol != null) { kontrol.close(); } return kontrol != null ? true : false; } public void opendatabase() throws sqlexception { // open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } public cursor sample_use_of_helper() { return mydatabase.query("table_name", null, null, null, null, null, null); } @override public synchronized void close() { if (mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } }
Comments
Post a Comment