【问题标题】:SQLite Database found in different directory在不同目录中找到 SQLite 数据库
【发布时间】:2019-03-13 05:17:44
【问题描述】:

一般 SQLite 数据库位于 /data/data/PACKAGE-NAME/databases/DBNAME 但我在一台设备的 /data/user/0/PACKAGE-NAME/databases/DBNAME 路径中找到了数据库。

那么,为什么这会发生在单个设备上?

谢谢。

【问题讨论】:

  • @shizhen 和 mesutpiskin 我现在在模拟器中签入,它包含两个目录中的数据库 - data/data/... & data/user/0/...。那么我必须使用哪一个?
  • 在某些设备中,/data/data 是指向/data/user/0 的符号链接,或者在某些设备中是反向链接。

标签: android sqlite


【解决方案1】:

您应该以编程方式获取数据库文件路径,而不是使用硬编码方式。请参阅getDatabasePath 的 API 文档;例如。

// for `Activity`, `Service`. Otherwise simply get the context.
Context context = this;
String dbname = "dummy.db";
String dbpath = context.getDatabasePath(dbname).getPath();
Log.d("MY_TAG", dbpath);

如果要获取数据库文件夹路径,请使用getParent(),如下所示:

String databaseFolderPath = context.getDatabasePath(dbname).getParent();

【讨论】:

    猜你喜欢
    • 2018-12-24
    • 2018-01-14
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多