【问题标题】:How to retrieve the name of database from a path in android如何从android中的路径中检索数据库的名称
【发布时间】:2014-07-10 02:51:36
【问题描述】:

我有一个名为 test.db 的数据库位于应用程序目录中。我使用“getPath()”方法检索了路径,该方法给出了 db 的整个路径。如何从该路径获取数据库的名称。 我会收到一个 data\test.db 的路径。我只需要得到 test.db 。如何仅从 android 中的路径中检索数据库的名称。

提前致谢

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    正如在另一个线程中解释的那样,您可以这样做:

     context.getDatabasePath(DataBaseHelper.dbName)
    

    如果我理解得很好,在您的情况下,名称是 test.db;这是正确的方法,我认为你会得到你期望的路径。

    来源:Get database path

    不管怎样;如果您只想从 String data\test.db 中提取名称,那么您可以:

    String path = yourpath;
    String dbName = path.substring(path.length - 8, 7)
    

    以这种方式使用 Substring 将始终提取最后 7 个字符。

    来源:Extract substring from a string

    【讨论】:

      【解决方案2】:

      试试这个

          String dbpath="yourdbpath";
          File f = new File(dbpath);
          String dbName = f.getName();
      

      【讨论】:

        【解决方案3】:

        split() 表示字符串splitting 喜欢

        String str=yourpathstring;
        String[] str1=str.split("/");
        String dbname=str1[1];
        

        【讨论】:

        • 在 str.split("\") 行插入缺少引号时出错
        • @Android_programmer_office 尝试反斜杠 str.split("/")
        猜你喜欢
        • 1970-01-01
        • 2012-06-05
        • 2014-09-11
        • 1970-01-01
        • 2022-11-24
        • 1970-01-01
        • 2012-07-06
        • 2011-05-09
        • 1970-01-01
        相关资源
        最近更新 更多