android - Premade database. Keeps forcing close. Sqlite errors -
i have made premade 1 , trying use asset folder keep getting errors.
05-02 14:58:44.771: e/database(573): sqlite3_open_v2("/data/data/com.mc.chempal/databases/chempaldb.db", &handle, 2, null) failed 05-02 14:58:44.792: e/androidruntime(573): fatal exception: main 05-02 14:58:44.792: e/androidruntime(573): java.lang.runtimeexception: unable start activity componentinfo{com.mc.chempal/com.mc.chempal.sqlview}: android.database.sqlite.sqliteexception: unable open database file 05-02 14:58:44.792: e/androidruntime(573): @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 05-02 14:58:44.792: e/androidruntime(573): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 05-02 14:58:44.792: e/androidruntime(573): @ android.app.activitythread.access$1500(activitythread.java:117) 05-02 14:58:44.792: e/androidruntime(573): @ android.app.activitythread$h.handlemessage(activitythread.java:931) 05-02 14:58:44.792: e/androidruntime(573): @ android.os.handler.dispatchmessage(handler.java:99) 05-02 14:58:44.792: e/androidruntime(573): @ android.os.looper.loop(looper.java:123) 05-02 14:58:44.792: e/androidruntime(573): @ android.app.activitythread.main(activitythread.java:3683) 05-02 14:58:44.792: e/androidruntime(573): @ java.lang.reflect.method.invokenative(native method) 05-02 14:58:44.792: e/androidruntime(573): @ java.lang.reflect.method.invoke(method.java:507) 05-02 14:58:44.792: e/androidruntime(573): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 05-02 14:58:44.792: e/androidruntime(573): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 05-02 14:58:44.792: e/androidruntime(573): @ dalvik.system.nativestart.main(native method) 05-02 14:58:44.792: e/androidruntime(573): caused by: android.database.sqlite.sqliteexception: unable open database file 05-02 14:58:44.792: e/androidruntime(573): @ android.database.sqlite.sqlitedatabase.dbopen(native method) 05-02 14:58:44.792: e/androidruntime(573): @ android.database.sqlite.sqlitedatabase.<init>(sqlitedatabase.java:1849) 05-02 14:58:44.792: e/androidruntime(573): @ android.database.sqlite.sqlitedatabase.opendatabase(sqlitedatabase.java:820) 05-02 14:58:44.792: e/androidruntime(573): @ com.mc.chempal.sqlstuff.opendatabase(sqlstuff.java:148) 05-02 14:58:44.792: e/androidruntime(573): @ com.mc.chempal.sqlview.oncreate(sqlview.java:20) 05-02 14:58:44.792: e/androidruntime(573): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 05-02 14:58:44.792: e/androidruntime(573): @ android.app.activitythread.performlaunchactivity(activitythread.java:1611) 05-02 14:58:44.792: e/androidruntime(573): ... 11 more
the log cats above go code below i'm trying use database made in sqlite browser have added android meta data , integer primary key present in database. causes app crash whenever try relating db.
package com.mc.chempal; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class sqlstuff extends sqliteopenhelper{ public static final string key_rowid= "_id"; public static final string key_sysname= "systematic_name"; public static final string key_molarmass= "molar_mass"; public static final string key_group = "groups"; public static final string key_mp ="melting_point"; public static final string key_bp ="boiling_point"; public static final string key_comname="general_name"; public static final string key_conelements="elements_included_c_h"; private sqlitedatabase chempal; private static string database_name = "chempaldb.db"; private static int database_version = 1; private string database_path = "/data/data/com.mc.chempal/databases/"; private static string database_table = "chemtable"; private sqlitedatabase sampledatabase; private final context androidcontext; /** * constructor * takes , keeps reference of passed context in order access application assets , resources. * @param context */ public sqlstuff(context context) { super(context, database_name, null, database_version); this.androidcontext = context; } /** * creates empty database on system , rewrites own database. * */ public void createdatabase() throws ioexception{ boolean dbexist = checkdatabase(); if(dbexist){ //do nothing - database exist } else { // create skeleton 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(){ sqlitedatabase checkdb = null; try{ string mypath = database_path + database_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); }catch(sqliteexception e){ //database does't exist yet. } if(checkdb != null){ checkdb.close(); } return checkdb != null ? true : false; } /** * 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 = androidcontext.getassets().open(database_name); // path created empty db string outfilename = database_path + database_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 = database_path + database_name; sampledatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } @override public synchronized void close() { if(sampledatabase != null) sampledatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo: write routines remove , overwrite database on upgrade db.execsql("drop table if exists" + database_table); oncreate(db); } /*** * sample function returns cursor database. should use data provider pattern, used brevity * @return */ public cursor getusertable() { sqlitedatabase db = getreadabledatabase(); cursor cursor = db.rawquery("select * user", null); return cursor; } public long createentry(string sysname, string molmass, string group, string mp, string bp, string comname, string conelements){ //todo auto-generated method stub try{ contentvalues cv = new contentvalues(); cv.put(key_sysname, sysname); cv.put(key_molarmass, molmass); cv.put(key_group, group); cv.put(key_mp, mp); cv.put(key_bp, bp); cv.put(key_comname, comname); cv.put(key_conelements, conelements); return chempal.insert(database_table, null, cv); } catch (exception e) { string tag = "my tag"; log.e(tag, e.tostring()); return 0; } } public string getdata() { // todo auto-generated method stub string[] columns = new string[] { key_rowid , key_sysname , key_molarmass ,key_group , key_mp , key_bp , key_comname , key_conelements }; cursor c = chempal.query(database_table, columns, null, null, null, null, null); string result = ""; int irow = c.getcolumnindex(key_rowid); int isysname = c.getcolumnindex(key_sysname); int imolarmass = c.getcolumnindex(key_molarmass); int igroup = c.getcolumnindex(key_group); int imp = c.getcolumnindex(key_mp); int ibp = c.getcolumnindex(key_bp); int icommonname = c.getcolumnindex(key_comname); int ielements = c.getcolumnindex(key_conelements); (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result = result + c.getstring(irow) + " " + c.getstring(isysname) + " " + c.getstring(imolarmass) + " " +c.getstring(igroup) + " " + c.getstring(imp)+ " " + c.getstring(ibp) + " "+ c.getstring(icommonname) + " " +c.getstring(ielements) + "\n"; } return result; } public string[] getidsysname() { string[] result = new string[0]; try { string[] columns = new string[] {key_sysname}; cursor c = chempal.query(database_table, columns, null, null, null, null, null); log.d("sqldstuff", "cursor count: "+c.getcount()); int isysname = c.getcolumnindex(key_sysname); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result[i] = c.getstring(isysname); i++; } } catch(exception e) { } return result; } public string[] getidgroup() { string[] result2 = new string[0]; try { string[] columns = new string[] { key_group }; cursor c = chempal.query(database_table, columns, null, null, null, null, null); int igroup = c.getcolumnindex(key_group); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result2 = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result2[i] = c.getstring(igroup); i++; } } catch(exception e) { } return result2; } public string[] getidmp() { string[] result3 = new string[0]; try { string[] columns = new string[] { key_mp }; cursor c = chempal.query(database_table, columns, null, null, null, null, null); int imp = c.getcolumnindex(key_mp); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result3 = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result3[i] = c.getstring(imp); i++; } } catch(exception e) { } return result3; } public string[] getidbp() { string[] result3 = new string[0]; try { string[] columns = new string[] { key_bp }; cursor c = chempal.query(database_table, columns, null, null, null, null, null); int ibp = c.getcolumnindex(key_bp); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result3 = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result3[i] = c.getstring(ibp); i++; } } catch(exception e) { } return result3; } public string[] getidcomname() { string[] result4 = new string[0]; try { string[] columns = new string[] { key_comname }; cursor c = chempal.query(database_table, columns, null, null, null, null, null); int icommonname = c.getcolumnindex(key_comname); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result4 = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result4[i] = c.getstring(icommonname); i++; } } catch(exception e) { } return result4; } public string[] getidelement() { string[] result5 = new string[0]; try { string[] columns = new string[] { key_conelements }; cursor c = chempal.query(database_table, columns, null, null, null, null, null); int ielements = c.getcolumnindex(key_conelements); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result5 = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result5[i] = c.getstring(ielements); i++; } } catch(exception e) { } return result5; } public string[] getidmolarmass() { string[] result6 = new string[0]; try { string[] columns = new string[] { key_molarmass }; cursor c = chempal.query(database_table, columns, null, null, null, null, null); int ielements = c.getcolumnindex(key_molarmass); int = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ i++; } result6 = new string[i]; = 0; (c.movetofirst(); !c.isafterlast(); c.movetonext()){ result6[i] = c.getstring(ielements); i++; } } catch(exception e) { } return result6; } }
are calling methods as
sqlstuff msqlstuff = new sqlstuff(youractivity.this); try { msqlstuff.createdatabase(); } catch (ioexception e) { e.printstacktrace(); } msqlstuff.opendatabase();
i think may have forgotten call create data base method.
Comments
Post a Comment