【问题标题】:push sqlite database file into phone将 sqlite 数据库文件推送到手机中
【发布时间】:2015-02-09 21:22:04
【问题描述】:

我需要将 sqlite 数据库文件推送到手机应用程序存储位置。 我为我的应用程序包数据库尝试了this。但不起作用。有什么办法吗?

我尝试在设备外壳中使用以下命令,但收到设备未找到消息。

shell@android: adb push MY_DB_FILE /data/data/MY_PACKAGE_NAME/databases/

【问题讨论】:

  • 你想将数据库从 Assest 复制到 sdcard
  • @NaveenTamrakar 在问我或告诉我路。请详细说明。
  • 你不能写信给/data/data/,除非你的手机已经root并且你有su,或者你的应用正在写数据库文件本身。

标签: android database sqlite android-sdcard


【解决方案1】:

只要您需要数据库文件,就调用如下所示的 write() 方法。它将在您设备的根文件夹中创建一个 backupname.db 文件。(您可以在 backupDBPath 字符串中更改 .db 文件的名称)

private void write() throws IOException {
    File sd = Environment.getExternalStorageDirectory();

    if (sd.canWrite()) {

        String currentDBPath = DatabaseHelper.DATABASE_NAME;
        String backupDBPath = "backupname.db";
        File currentDB = new File(getDBPath(), currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) {
            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());

            src.close();
            dst.close();

        }
    }
}

private String getDBPath() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
    } else {
        return getFilesDir().getPath() + getPackageName() + "/databases/";
    }
}

【讨论】:

  • 谁投了反对票,你能告诉我这有什么问题吗,因为它在我的项目中运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 2012-05-03
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多