【问题标题】:Android: No such file or directoryAndroid:没有这样的文件或目录
【发布时间】:2014-06-02 12:04:00
【问题描述】:

我已经创建了我的 sqlite 数据库 (employeertest.sql) 并将它放在 Assets 文件夹中,然后运行 这段代码。

不幸的是在getBaseContext().getAssets().open("employeertest")编译器说No such file or directory

 try {
                String destPath = "/data/data/" + getPackageName() +"/databases";
                File f = new File(destPath);

                    f.mkdirs();
                    f.createNewFile();

                    //---copy the db from the assets folder into 
                    // the databases folder---


                        CopyDB(getBaseContext().getAssets().open("employeertest"),
                        new FileOutputStream(destPath + "/employeertest"));

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

【问题讨论】:

  • 尝试在目录字符串中使用双斜杠或双反斜杠作为分隔符。
  • 试试getBaseContext().getAssets().open("employeertest.sql")...你需要文件扩展名。
  • @JanBo 好的,它解决了我的问题,但现在它说no such table : test 用于查询select * from test
  • 好吧,你要么在你的数据库中没有那个表 :) 我非常怀疑,要么你从文件中正确地初始化了数据库。把数据库打开代码和查询代码发给你。
  • @JanBo 实际上我有,我使用 firefox 插件创建了我的数据库。请看一下这个链接。 stackoverflow.com/questions/934237/…

标签: java android file


【解决方案1】:

你试过了吗

CopyDB(getBaseContext().getAssets().open("employeertest.sql")

相反?考虑到该文件不存在,我假设扩展名丢失并且找不到该文件。

【讨论】:

    【解决方案2】:

    您可以使用Context.getDatabasePath(String) 检索数据库目录的正确路径。请注意,当您尝试从资产中复制数据库时,该目录也可能不存在

    【讨论】:

      猜你喜欢
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      相关资源
      最近更新 更多