i new android development,so have existing database called courses,and databasehelper file copy existing file assets folder in android studio runs loop , creates empty database android_manifest file in ,"no tables copied".so therefore giving me , error of no table found when running select table.
error: e/androidruntime: fatal exception: main process: com.example.sinhanikita17.myapplication, pid: 15127 java.lang.runtimeexception: unable start activity componentinfo{com.example.sinhanikita17.myapplication/com.example.sinhanikita17.myapplication.choose}: android.database.sqlite.sqliteexception: no such table: ics (code 1): ,
public class databasehelper extends sqliteopenhelper { private static string db_path;
private static string db_name = "courses.db"; private sqlitedatabase mydatabase; private final context mycontext; private static final string logtag = "myapp5"; private static databasehelper _dbhelper; public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; db_path = "/data/data/" + context.getpackagename() + "/databases/"; log.i(logtag, db_path); } public static databasehelper getinstance(context context) { if(_dbhelper == null) { _dbhelper = new databasehelper(context); } return _dbhelper; } public void createdatabase() throws ioexception{ boolean dbexist = checkdatabase(); if(dbexist){ //do nothing - database exist }else{ //by 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"); } } } private boolean checkdatabase(){ sqlitedatabase checkdb = null; try{ string mypath = db_path + db_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); }catch(sqliteexception e){ //database doesn't exist yet. } if(checkdb != null) checkdb.close(); return checkdb != null ? true : false; } private void copydatabase() throws ioexception{ //open local db input stream inputstream myinput = mycontext.getassets().open("courses.db"); log.i(logtag, string.valueof(mycontext.getassets())); // 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(); }
Comments
Post a Comment