Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot create SQL database from downloaded file which is saved in /data/data/appname/files
    primarykey
    data
    text
    <p>I tried searching all around but cannot find a definite solution.</p> <p>What I trying to do is to download an always updated with new inputs SQL file in XML format from URL and save it in the /data/data/appprogram/files/ folder. The downloading of file is completed and tested confirmed file is downloaded into emulator through DDMS file explorer.</p> <p>Next I want to open this file to get all the statements and parse it into a database defined as camera_database which is located in /data/data/appprogram/databases. This is where the problem begins. I cannot get beyond this point. </p> <p>I tried to input the data using both inputstream and openfileinput but not working.</p> <p>Below is the code I used. Please advise from any kind soul..</p> <pre><code>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"); for (int i=0; i&lt;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); } } </code></pre> <p>thanks for the response. wish to add on, i do some test to ensure my file can be feed into /databases/ folder. i placed the alladvertlist.xml in the res/raw folder in startup, do a inputstream using</p> <pre><code>InputStream in = camera_context.getResources().openRawResource(R.raw.alladvertlist); </code></pre> <p>which works fine, i can retrieve the SQL data from /databases/camera_database with above command.</p> <p>But it just fails to parse the file into /databases/camera_database when i download the file from url to a specific folder /data/data/appprogram/alladvertlist.xml and use inputstream to extract the data within to documentbuilder.</p> <p>below is the code for downloading file from URL and save in a specific folder which works fine. For all to reference and use if you find it useful.</p> <pre><code> /** Download a specified file from the web */ public void updateadvData(){ final String file_url = this.getString(R.string.alladvertlisturl); /* Define and configure the 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 the progress dialog. */ myProgress.show(); /* Download our file */ new Thread(new Runnable(){ public void run(){ try { //create the new connection URL url = new URL(file_url); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //set up some things on the 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 like parent directory does not exist..."); if(!parentDirectory.mkdirs()) { System.err.println("And we cannot create it..."); // we have to return, throw or something 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 will be used to write the downloaded data into the file we created FileOutputStream fileOutput = new FileOutputStream(file); //this will be used in reading the data from the internet InputStream inputStream = urlConnection.getInputStream(); //this is the total size of the file int totalSize = urlConnection.getContentLength(); myProgress.setMax(totalSize); //variable to store total downloaded bytes int downloadedSize = 0; //create a buffer... byte[] buffer = new byte[1024]; int bufferLength = 0; //used to store a temporary size of the buffer int progress = 0; //now, read through the input buffer and write the contents to the file while ( (bufferLength = inputStream.read(buffer)) &gt; 0 ) { //add the data in the buffer to the file in the file output stream (the file on the sd card fileOutput.write(buffer, 0, bufferLength); //add up the size so we know how much is downloaded downloadedSize += bufferLength; //Here we update the progress progress = downloadedSize; myProgress.setProgress(progress); } //close the output stream when done fileOutput.close(); myProgress.dismiss(); //catch some possible errors... } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } </code></pre> <p>i tried searching has it to do with byte or string when i do inputstream to documentbuilder but seems like it is not really the cause. below is the structure of SQL file downloadable from url, eliminating the contents</p> <pre><code>&lt;sql&gt; &lt;statement&gt; .... &lt;/statement&gt; &lt;/sql&gt; 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 with 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 to start activity ComponentInfo{advertisebiz.gadgetscan/advertisebiz.gadgetscan.AdvPage02}: android.database.sqlite.SQLiteException: no such table: alladvert (code 1): , while compiling: SELECT _id, advcategory FROM alladvert WHERE advcategory = ? </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COyou might want to share the exact error message(s) returned to you. some wild guess: is the first of your sql statements affecting the `alladvert` table the one to create it?
      singulars
    2. COi editted the post. i rebuild the app ,putting the file in res/raw for the xml file that is to be downloaded. it works fine when i use inputstream directing to res/raw/. it may just mean the file is okay for parsing in. so the issue is when i try to parse the file the downloaded one from URL. can it be that the input stream command InputStream in = camera_context.getApplicationContext().openFileInput("/data/data/appprogram/files/alladvertlist.xml"); is giving problem? because i checked form DDMS, the file in database was not updated / parse from the file..
      singulars
    3. COI've been considering a similar implementation for an upcoming task and was planning on parsing the file in the second way that you tried (locally, from raw/xml). I imagined this way would be less dependent on network connectivity. Then I'd periodically check the device's access state and overwrite the local file based on updates from the URL file. Would this approach meet your requirements?
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload