sqlite - android database returning no records but sqlitebrowser contains data -
i have problem database file not being read have added database file in assets called mydb when run code says not being located. calling toast toast.maketext(this, "no contact found", toast.length_long).show(); being called because no records being returned. know finding file there no filenotfoundexception exception. example form android application development book.
public class databaseactivity extends activity { /** called when activity first created. */ textview quest, response1, response2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); textview quest = (textview) findviewbyid(r.id.quest); try { string destpath = "/data/data/" + getpackagename() + "/databases/mydb"; file f = new file(destpath); if (!f.exists()) { copydb( getbasecontext().getassets().open("mydb"), new fileoutputstream(destpath)); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } dbadapter db = new dbadapter(this); //---get contact--- db.open(); cursor c = db.getcontact(2); if (c.movetofirst()) displaycontact(c); else toast.maketext(this, "no contact found", toast.length_long).show(); db.close(); } public void copydb(inputstream inputstream, outputstream outputstream) throws ioexception { //---copy 1k bytes @ time--- byte[] buffer = new byte[1024]; int length; while ((length = inputstream.read(buffer)) > 0) { outputstream.write(buffer, 0, length); } inputstream.close(); outputstream.close(); } public void displaycontact(cursor c) { quest.settext(string.valueof(c.getstring(1))); //quest.settext(string.valueof("this text string")); } } is there better way upload data.
a couple of things come mind here...
because of
!f.exists()check, once database exists (and maybe empty) never copy again. maybe now, copy time, until work out kinks , add in!f.exists()i've had mixed results
e.printstacktrace(), maybe changelog.e(tag, "message", e), see if start seeing errors showing in logcat
as better way... i've done couple different ways... 1. create file (json, cvs, etc) , process , load it, if database empty 2. similar first, except create java serialized object array , load database, if database empty.
also don't know dbadapter looks like, , since wraps database issue may there.
Comments
Post a Comment