【问题标题】:Android SQLite: where do I copy .sqlite file into on first run?Android SQLite:我在第一次运行时将 .sqlite 文件复制到哪里?
【发布时间】:2011-06-19 08:22:16
【问题描述】:

我在关注Using your own SQLite database in your Android applications,在教程中作者提到我需要将sqlite文件复制到“默认系统路径”中:

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";

方法是:

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

我还阅读了该主题: Do all Android devices have an internal SD card

接受的答案是:

任何具有 Android 电子市场的设备都将在 Environment.getExternalStorageDirectory() 中拥有至少 2GB 的存储空间。是 SD 卡还是其他东西会因设备而异。

所以我想知道:

  1. “默认系统路径”是否与 Environment.getExternalStorageDirectory() 相同
  2. 我需要用 Environment.getExternalStorageDirectory() + “/YOUR_PACKAGE/databases”替换“/data/data/YOUR_PACKAGE/databases”吗?

我做了一个测试,在 Motorola Milestone(带有物理 QWERTY 键盘的那个)上打印出 Environment.getExternalStorageDirectory() 的结果并返回:

Dir: /sdcard

所以它们不一样,但是我应该使用哪一个来用于 SQLite 数据库?

谢谢!

【问题讨论】:

    标签: android sqlite directory


    【解决方案1】:

    如前所述,databae 是在/data/data/YOUR_PACKAGE/databases/ 创建的,看here getDatabasePath()方法返回默认的数据库路径,非常方便。

    【讨论】:

      【解决方案2】:

      Android 应用使用"/data/data/YOUR_PACKAGE/databases/" 作为数据库文件夹。当您创建扩展 SQLiteOpenHelper 的类时,它将在此文件夹中查找您的表。

      sdcard 可以用来存储额外的数据,但正如你所说,并不是所有的 android 设备都有一个。

      问候, 斯蒂芬

      【讨论】:

        猜你喜欢
        • 2013-12-22
        • 1970-01-01
        • 2014-01-31
        • 1970-01-01
        • 2014-06-05
        • 1970-01-01
        • 2014-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多