java - Cannot create SQL database from downloaded file which is saved in /data/data/appname/files -
i tried searching around cannot find definite solution.
what trying download updated new inputs sql file in xml format url , save in /data/data/appprogram/files/ folder. downloading of file completed , tested confirmed file downloaded emulator through ddms file explorer.
next want open file statements , parse database defined camera_database located in /data/data/appprogram/databases. problem begins. cannot beyond point.
i tried input data using both inputstream , openfileinput not working.
below code used. please advise kind soul..
public class help_databasehelperadv extends sqliteopenhelper { public static final string camera_database_name = "camera_database"; protected context camera_context; public help_databasehelperadv(context context) { super(context, camera_database_name, null, 1); this.camera_context = context; } @override public void oncreate(sqlitedatabase cameradb) { string s; try { inputstream in = camera_context.getapplicationcontext().openfileinput("/data/data/appprogram/files/alladvertlist.xml"); documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder(); document doc = builder.parse(in, null); nodelist statements = doc.getelementsbytagname("statement"); (int i=0; i<statements.getlength(); i++) { s = statements.item(i).getchildnodes().item(0).getnodevalue(); cameradb.execsql(s); } } catch (throwable t) { toast.maketext(camera_context, t.tostring(), 50000).show(); } } @override public void onupgrade(sqlitedatabase cameradb, int oldversion, int newversion) { cameradb.execsql("drop table if exists alladvert"); oncreate(cameradb); } }
thanks response. wish add on, test ensure file can feed /databases/ folder. placed alladvertlist.xml in res/raw folder in startup, inputstream using
inputstream in = camera_context.getresources().openrawresource(r.raw.alladvertlist);
which works fine, can retrieve sql data /databases/camera_database above command.
but fails parse file /databases/camera_database when download file url specific folder /data/data/appprogram/alladvertlist.xml , use inputstream extract data within documentbuilder.
below code downloading file url , save in specific folder works fine. reference , use if find useful.
/** download specified file web */ public void updateadvdata(){ final string file_url = this.getstring(r.string.alladvertlisturl); /* define , configure progress dialog */ final progressdialog myprogress = new progressdialog(this); myprogress.setprogressstyle(progressdialog.style_horizontal); myprogress.setmessage(this.getstring(r.string.dialog_text)); myprogress.settitle(r.string.dialog_title); /* show progress dialog. */ myprogress.show(); /* download our file */ new thread(new runnable(){ public void run(){ try { //create new connection url url = new url(file_url); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); //set things on connection urlconnection.setrequestmethod("get"); urlconnection.setdooutput(true); //and connect! urlconnection.connect(); file parentdirectory = new file("/data/data/appprogram/files"); if(!parentdirectory.exists()) { system.err.println("it seems parent directory not exist..."); if(!parentdirectory.mkdirs()) { system.err.println("and cannot create it..."); // have return, throw or else } } // file file = new file(sdcardroot,"filename.sqlite"); file file = new file(parentdirectory, "alladvertlist.xml"); if(file.exists()) { system.err.println("alladvertlist deleting..."); file.delete(); } file.createnewfile(); //this used write downloaded data file created fileoutputstream fileoutput = new fileoutputstream(file); //this used in reading data internet inputstream inputstream = urlconnection.getinputstream(); //this total size of file int totalsize = urlconnection.getcontentlength(); myprogress.setmax(totalsize); //variable store total downloaded bytes int downloadedsize = 0; //create buffer... byte[] buffer = new byte[1024]; int bufferlength = 0; //used store temporary size of buffer int progress = 0; //now, read through input buffer , write contents file while ( (bufferlength = inputstream.read(buffer)) > 0 ) { //add data in buffer file in file output stream (the file on sd card fileoutput.write(buffer, 0, bufferlength); //add size know how downloaded downloadedsize += bufferlength; //here update progress progress = downloadedsize; myprogress.setprogress(progress); } //close output stream when done fileoutput.close(); myprogress.dismiss(); //catch possible errors... } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }).start(); }
i tried searching has byte or string when inputstream documentbuilder seems not cause. below structure of sql file downloadable url, eliminating contents
<sql> <statement> .... </statement> </sql> 05-15 01:51:13.347: e/sqlitelog(1391): (1) no such table: alladvert 05-15 01:51:13.347: d/androidruntime(1391): shutting down vm 05-15 01:51:13.347: w/dalvikvm(1391): threadid=1: thread exiting uncaught exception (group=0x40a71930) 05-15 01:51:13.377: e/androidruntime(1391): fatal exception: main 05-15 01:51:13.377: e/androidruntime(1391): java.lang.runtimeexception: unable start activity componentinfo{advertisebiz.gadgetscan/advertisebiz.gadgetscan.advpage02}: android.database.sqlite.sqliteexception: no such table: alladvert (code 1): , while compiling: select _id, advcategory alladvert advcategory = ?
Comments
Post a Comment