android - Data Not Preserved in SQLite after the App Restarted -
my sqlite database not preserving stored data.i getting huge trouble this.i have not found suitable solution this.please 1 me out of this. want store data after app gets restarted. posting dbhelper class , main method.please help.i want preserve data. dbhelper class.
my dbhelper class:
public class dbhelper extends sqliteopenhelper { private static string db_name = "photodb"; private sqlitedatabase mydatabase; private dbhelper db; public context mycontext; file dbfile; file dbfilepath; assetmanager myassest; assetfiledescriptor me; cursor ch; public dbhelper(context mycontext) { super(mycontext, db_name, null, 1); this.mycontext = mycontext; system.out.println("context value:--"+mycontext); string state=environment.getexternalstoragestate(); system.out.println("my storage state:-"+state); } public void createdatabase(context mycontext) throws ioexception{ boolean dbexist = checkdatabase(); if(dbexist){ system.out.println("yes"); string path = dbfile.getabsolutepath(); system.out.println("my database path:--"+path); } if(!dbexist) { dbfile= new file(environment.getexternalstoragedirectory().getabsolutepath()+"/"+"photoapp"); boolean flag=dbfile.mkdir(); dbfilepath = new file(environment.getexternalstoragedirectory().getabsolutepath()+"/"+"photoapp"+"/"+"photodb"); dbfilepath.createnewfile(); system.out.println("not..."+flag); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } public boolean checkdatabase() { dbfile = new file(environment.getexternalstoragedirectory().getabsolutepath()+"/"+"photodb"); return dbfile.exists(); } private void copydatabase() throws ioexception{ inputstream myinput = mycontext.getassets().open("photodb"); string outfilename = dbfilepath.getabsolutepath(); 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(); } public void opendatabase() throws sqlexception{ string mypath = environment.getexternalstoragedirectory().getabsolutepath()+"/"+"photoapp"+"/"+"photodb" ; system.out.println("my database path:----"+mypath); mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } @override public synchronized void close() { if(mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public synchronized sqlitedatabase getwritabledatabase() { return super.getwritabledatabase(); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } public void add(string imagepath,string description,string location) { long rowid; contentvalues myvalue = new contentvalues(); myvalue.put("imagepath",imagepath); myvalue.put("description", description); myvalue.put("location", location); system.out.println("my image path:-"+imagepath+"my description:--"+description+"my location:--"+location); string sql = "insert photodetails values(null,'"+imagepath+"','"+description+"','"+location+"');"; mydatabase.execsql(sql); rowid = mydatabase.insert("photodetails", "id", myvalue); system.out.println("my row id is:----"+rowid); system.out.println("db updated"); } public arraylist<string> getimages(){ ch = mydatabase.rawquery("select imagepath photodetails" , null); arraylist <string> imageloc = new arraylist<string>(); if(ch.getcount() > 0){ ch.movetofirst(); { imageloc.add(ch.getstring(0)); }while(ch.movetonext()); } return imageloc; } public arraylist<string> getdescriptions(){ ch = mydatabase.rawquery("select description photodetails" , null); arraylist <string> des = new arraylist<string>(); if(ch.getcount() > 0){ ch.movetofirst(); { des.add(ch.getstring(0)); }while(ch.movetonext()); } return des; } public arraylist<string> getlocations(){ ch = mydatabase.rawquery("select location photodetails" , null); arraylist <string> local = new arraylist<string>(); if(ch.getcount() > 0){ ch.movetofirst(); { local.add(ch.getstring(0)); }while(ch.movetonext()); } return local; } }
this mainactivity :
public class mainactivity extends activity { dbhelper mydb; context cont; arraylist<string> myval = new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mydb = new dbhelper(mainactivity.this); try { mydb.createdatabase(mainactivity.this); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } mydb.opendatabase(); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); mydb.add("helloworld", "done", "yes"); myval = mydb.getimages(); int i=myval.size(); system.out.println("size:----"+i); finish(); // mydb.opendatabase(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
change method
public boolean checkdatabase() { dbfile = new file(environment.getexternalstoragedirectory().getabsolutepath()+"/"+"photodb"); return dbfile.exists(); }
to
public boolean checkdatabase() { dbfile = new file(environment.getexternalstoragedirectory().getabsolutepath()+"/"+"photoapp"); return dbfile.exists(); }
check , let me know
Comments
Post a Comment