sqlite - Android - Could not open database -
i have db.sqlite
file in asset
folder , error when try open it:
android.database.sqlite.sqlitecantopendatabaseexception: unknown error (code 14): not open database
i've tried different paths , names (with , without extension) can't make work.
package com.example.app.db; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import android.content.context; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.database.sqlite.sqliteopenhelper; import android.widget.toast; public class databasehelper extends sqliteopenhelper { // android's default system path of application database. private static string db_path = "/data/data/com.example.app/databases/"; private static string db_name = "app"; private sqlitedatabase mydatabase; private final context mycontext; /** * constructor takes , keeps reference of passed context in order * access application assets , resources. * * @param context */ public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } /** * creates empty database on system , rewrites own * database. * */ public void createdatabase() throws ioexception { boolean dbexist = checkdatabase(); if (dbexist) { // nothing - database exist } else { // calling method , empty database created // default system path // of application gonna able overwrite // database our database. this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } /** * check if database exist avoid re-copying file each * time open application. * * @return true if exists, false if doesn't */ private boolean checkdatabase(){ boolean checkdb = false; try{ string mypath = mycontext.getfilesdir().getabsolutepath().replace("files", "databases")+file.separator + db_name; file dbfile = new file(mypath); checkdb = dbfile.exists(); } catch(sqliteexception e){ system.out.println("database doesn't exist"); } return checkdb; } /** * copies database local assets-folder created * empty database in system folder, can accessed , * handled. done transfering bytestream. * */ 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(); } public void opendatabase() throws sqlexception{ //open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } @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) { } }
i error when try open here:
package com.example.app; import android.app.activity; import android.os.bundle; import android.widget.textview; import com.example.app.db.databasehelper; public class speakerslistactivity extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mylayout); databasehelper dbhelper = new databasehelper(this); dbhelper.opendatabase(); } }
you need check existence of database file first not doing here
databasehelper dbhelper = new databasehelper(this); dbhelper.opendatabase();
you making object of databasehelper , calling .opendatabase()
try constructor :
public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; try{ string mypath = db_path + db_name; // check extension of db file file dbfile = new file(mypath); if( dbfile.exists()); toast.maketext(context, "database exists", toast.length_long).show(); else toast.maketext(context, "cant find database", toast.length_long).show(); } catch(sqliteexception e){ system.out.println("database doesn't exist"); } }
Comments
Post a Comment