【问题标题】:android - restore sqlite database from internal storge to appicationandroid - 将 sqlite 数据库从内部存储恢复到应用程序
【发布时间】:2018-08-29 11:52:32
【问题描述】:

我正在尝试从手机存储中恢复数据库,以便在我的应用程序中使用它。 我成功地将我的数据库备份到内部存储,但是当我尝试恢复它时,会弹出以下 io.exception

java.io.IOException:打开失败:ENOENT(没有这样的文件或目录)

我该如何解决这个问题? 但是,我尝试了以下现有解决方案,但没有奏效 Android - java.io.FileNotFoundException, is it possible backup and RESTORE a database file in android? non root devices, Restoring SQLite DB file

private void restoreDatabase(Context context) {
    String packagename = getPackageName();

    File sdcard = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    try {
        if (sdcard.canWrite()) {
            String currentDBPath = "//data/"+getPackageName() +"/databases/" + DATABASE_NAME;
            String backupDBPath = DATABASE_NAME;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sdcard, backupDBPath);
            currentDB.createNewFile();

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(backupDB).getChannel();
                FileChannel dst = new FileOutputStream(currentDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    }
}

请不要将此问题标记为重复

【问题讨论】:

  • 您想将现有数据库导入您的应用数据并稍后使用吗?
  • @fatemehfallahiarezoudar 是的,这正是我想做的事

标签: android sqlite android-sqlite


【解决方案1】:

导出数据库后,您可以使用此功能导入数据库,我有它 testet 并且工作正常:

private void importDB() {
        String appDataPath = getApplicationContext().getApplicationInfo().dataDir;

        File dbFolder = new File(appDataPath + "/databases");//Make sure the /databases folder exists
        dbFolder.mkdir();//This can be called multiple times.
        //EDITED
        File dbFilePath = new File(appDataPath + "/databases/"+"yourDataBaseName");

        try {
        //EDITED
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "YourBackupFolder","yourDataBaseFileName);
        FileInputStream inputStream = new FileInputStream(file); //use your database name
            OutputStream outputStream = new FileOutputStream(dbFilePath);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer))>0)
            {
                outputStream.write(buffer, 0, length);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();

        } catch (IOException e){
            //handle

            e.printStackTrace();
        }
    }

【讨论】:

  • 它对我不起作用。我的数据库存在于手机存储中,我想将其导入到我的应用程序中。您的代码在 InputStream inputStream = getApplicationContext().getAssets().open(DATABASE_NAME); 上给了我一个 IO 异常;
  • @Mohamad 我已经编辑了我的答案,请参阅新的 dbFilePath。
  • @Mohamad 您应该使用自己的数据库名称而不是 DATABASE_NAME
  • 我将数据库名称存储在 DATABASE_NAME 中。我认为你颠倒了流,输入流应该指向外部存储上的 db,输出流应该是应用程序中的 db 文件
  • 谢谢,代码运行良好,我编辑它以修复一个小错误
猜你喜欢
  • 1970-01-01
  • 2015-06-19
  • 2015-08-29
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 2017-12-19
相关资源
最近更新 更多