Focus on IT Recommend

Home > java - Cannot create SQL database from downloaded file which is saved in /data/data/appname/files

java - Cannot create SQL database from downloaded file which is saved in /data/data/appname/files



I tried searching all around but cannot find a definite solution.

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.

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.

I tried to input the data using both inputstream and openfileinput but not working.

Below is the code I used. Please advise from any 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;

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<statements.getLength(); i++) {
            s = statements.item(i).getChildNodes().item(0).getNodeValue();
    } catch (Throwable t) {
        Toast.makeText(camera_context, t.toString(), 50000).show();

public void onUpgrade(SQLiteDatabase cameradb, int oldVersion, int newVersion) {
    cameradb.execSQL("DROP TABLE IF EXISTS alladvert");


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

InputStream in = camera_context.getResources().openRawResource(R.raw.alladvertlist);

which works fine, i can retrieve the SQL data from /databases/camera_database with above command.

Recommend:sql - which database for java to store data in memory

Basically I want to store in the memory everything except some rarely used data (history of changes, etc). I'm looking for: simple API for java, preferably non-ORM ACID is not required (well, D is) some support for queries, but nothing fan

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.

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.

    /** 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);
    /* Show the progress dialog. */;

    /* 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

        //and connect!

        File parentDirectory = new File("/data/data/appprogram/files");
            System.err.println("It seems like parent directory does not exist...");
                 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");
            System.err.println("Alladvertlist deleting...");


        //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();
        //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 = > 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;

        //close the output stream when done
      //catch some possible errors...
    } catch (MalformedURLException e) {
    } catch (IOException e) {


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

<statement> .... </statement>

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 = ?
java android sql inputstream
edited May 15 '13 at 1:58 asked May 14 '13 at 15:35 user2380669 6 2      you might want to
  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? –  collapsar May 14 '13 at 16:11      i 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/file‌​s/alladvertlist.xml"‌​); is giving problem? because i checked form DDMS, the file in database was not updated / parse from the file.. –  user2380669 May 15 '13 at 2:00      I'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? –  es0329 May 15 '13 at 2:09      I am not very familiar with sQL and schema terms. i tried to understand but can`t . in this sql.xml, i have defined the columns in the first statement, and subsequent rows are populated with data.. –  user2380669 May 15 '13 at 2:37      yes, that will also achieve the goal i set forth. Although i prefer to have the data updated on first load that means not getting the data from raw/res. i kept on searching, has it to do with bytes and string when i feed into documentbuilder. on download of file, the inputstream is in bytes. sending the stream to documentbuilder is in byte too. but my on this command s = statements.item(i).getChildNodes().item(0).getNodeValue();, it is to be in string since s is defined as String at top. –  user2380669 May 15 '13 at 2:42  |  show more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Recommend:mysql - Update fields in SQL database with data from CSV file

e that answered mine in full. First off, I am a SQL novice at best. I've learned quite a bit since setting up my online store, but I'm still nowhere near knowledgeable on much of any of it. Okay, I have a database on my web server for my on


------splitte line----------------------------