Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从保存在/data/data/appname/files中的下载文件创建SQL数据库_Java_Android_Sql_Inputstream - Fatal编程技术网

Java 无法从保存在/data/data/appname/files中的下载文件创建SQL数据库

Java 无法从保存在/data/data/appname/files中的下载文件创建SQL数据库,java,android,sql,inputstream,Java,Android,Sql,Inputstream,我试着四处寻找,但找不到确切的解决办法 我试图做的是从URL下载一个XML格式的总是更新为新输入的SQL文件,并将其保存在/data/data/appprogram/files/文件夹中。文件下载完成,测试确认文件通过DDMS文件浏览器下载到emulator中 接下来,我想打开这个文件来获取所有语句,并将其解析到一个定义为camera_database的数据库中,该数据库位于/data/data/appprogram/databases中。这就是问题的开始。我无法超越这一点 我尝试同时使用inp

我试着四处寻找,但找不到确切的解决办法

我试图做的是从URL下载一个XML格式的总是更新为新输入的SQL文件,并将其保存在/data/data/appprogram/files/文件夹中。文件下载完成,测试确认文件通过DDMS文件浏览器下载到emulator中

接下来,我想打开这个文件来获取所有语句,并将其解析到一个定义为camera_database的数据库中,该数据库位于/data/data/appprogram/databases中。这就是问题的开始。我无法超越这一点

我尝试同时使用inputstream和openfileinput输入数据,但不起作用

下面是我使用的代码。请从任何善良的灵魂那里得到建议

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<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);
}

}
这很好,我可以使用上面的命令从/databases/camera_数据库检索SQL数据

但当我将文件从url下载到特定文件夹/data/data/appprogram/alladvertlist.xml并使用inputstream将其中的数据提取到documentbuilder时,它无法将文件解析为/databases/camera_database

下面是从URL下载文件并保存在特定文件夹中的代码,该文件夹工作正常。供大家参考和使用,如果你觉得有用的话

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

}
当我向documentbuilder输入Stream时,我尝试搜索与字节或字符串有关,但似乎这不是真正的原因。下面是可从url下载的SQL文件的结构,消除了内容

<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 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 = ?

.... 
05-15 01:51:13.347:E/SQLiteLog(1391):(1)没有这样的表:alladvert
05-15 01:51:13.347:D/AndroidRuntime(1391):关闭虚拟机
05-15 01:51:13.347:W/dalvikvm(1391):threadid=1:线程退出时出现未捕获异常(组=0x40a71930)
05-15 01:51:13.377:E/AndroidRuntime(1391):致命异常:主
05-15 01:51:13.377:E/AndroidRuntime(1391):java.lang.RuntimeException:无法启动活动组件信息{advertisebiz.gadgetscan/advertisebiz.gadgetscan.AdvPage02}:android.database.sqlite.SQLiteException:没有这样的表:alladvert(代码1):,编译时:从alladvert选择_id,advcategory,其中advcategory=?

您可能希望共享返回给您的确切错误消息。一些猜测:您的第一条sql语句是否影响了
alladvert
表?我编辑了这篇文章。我重新构建应用程序,将文件放入res/raw中,以便下载xml文件。当我使用inputstream定向到res/raw/时,它工作得很好。这可能只是意味着该文件可以在中进行解析。所以问题是当我试图解析从URL下载的文件时。是不是输入流命令InputStream in=camera_context.getApplicationContext().openFileInput(“/data/data/appprogram/files/alladvertlist.xml”);给予有问题吗?因为我检查了表单DDMS,所以数据库中的文件没有从该文件更新/解析..我一直在考虑为即将到来的任务使用类似的实现,并计划使用您尝试的第二种方法(本地,从raw/xml)解析该文件。我认为这种方式对网络连接的依赖性会降低。然后我会定期检查设备的访问状态,并根据URL文件的更新覆盖本地文件。这种方法能满足您的需求吗?我不太熟悉sQL和模式术语。我试着去理解,但无法理解。在这个sql.xml中,我在第一条语句中定义了列,随后的行中填充了数据。是的,这也将实现我设定的目标。虽然我更喜欢在第一次加载时更新数据,这意味着不从原始/存储中获取数据。我一直在搜索,这与我输入documentbuilder时的字节和字符串有关。下载文件时,输入流以字节为单位。将流发送到documentbuilder也是以字节为单位的。但是,在这个命令上,my=statements.item(i).getChildNodes().item(0).getNodeValue();,因为s在顶部被定义为字符串,所以它应该是字符串。
<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 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 = ?