导入/覆盖数据库SQlite for Android app

时间:2015-11-17 09:56:56

标签: java android database sqlite import

导入现有数据库(Sqlite)时遇到了一些问题。 导出工作正常,但当我将代码转移到导入版本时没有任何反应!没有错误 - 但也没有效果!

我希望用户将他/她的数据传输到新智能手机。他将复制新智能手机下载目录中的旧数据库。 代码运行后,不会覆盖新数据库。数据库仍然是空的。我在'DB浏览器中查看了Sqlite'

我希望你能帮助我!这是我的代码的两个版本。 try-block是不同的。

首先尝试:

private void importBackupDatabase(){
    readyToImport = true;

    //DataBackup
    File exS = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);// storage location of backup database
    FileChannel backupChannel= null;
    String backupName = "steps.db"; // file name, inside of download directory
    File backupDB = new File(exS, backupName);// erstelle File aus downloadorder und der datei /downloads/steps.db

    //TransferBackupTo
    String databasePath;
    if(android.os.Build.VERSION.SDK_INT >= 17){
        databasePath = context.getApplicationInfo().dataDir + "/databases/";
    }else{
        databasePath = "/data/data/" + context.getPackageName() + "/databases/";
    }

    File destinationDir = new File(databasePath);//directory

    FileChannel destinationChannel = null; // file channel to manage new db location

   File destinationDB = new File(destinationDir, "steps.db");//file

    try{
        backupChannel = new FileInputStream(backupDB).getChannel(); // file channel to manage backup location

        FileInputStream testInput = new FileInputStream(backupDB);

        FileOutputStream out = new FileOutputStream(destinationDB);
        destinationChannel = out.getChannel();

        byte[] buffer = new byte[1024];
        ByteBuffer bbuffer = ByteBuffer.wrap(buffer);
        int transferLength;

        while ((transferLength = testInput.read(buffer)) > 0)
        {
            destinationChannel.write(bbuffer);
        }

        destinationChannel.force(true);
        destinationChannel.close();
        backupChannel.close();

        Database.getInstance(context).close();
    }catch(IOException e){
        logger.severe("An error as occured. Overwritung DataBase canceled!");
    }
}

第二次尝试:

  private void importBackupDatabase(){
    readyToImport = true;

    //DataBackup
    File exS = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);// storage location of backup database
    FileChannel backupChannel= null;
    String backupName = "steps.db"; // file name, inside of download directory
    File backupDB = new File(exS, backupName);// erstelle File aus downloadorder und der datei /downloads/steps.db

    //TransferBackupTo
    String databasePath;
    if(android.os.Build.VERSION.SDK_INT >= 17){
        databasePath = context.getApplicationInfo().dataDir + "/databases/";
    }else{
        databasePath = "/data/data/" + context.getPackageName() + "/databases/";
    }

    File destinationDir = new File(databasePath);//directory

    FileChannel destinationChannel = null; // file channel to manage new db location

   File destinationDB = new File(destinationDir, "steps.db");//file

    try{
        backupChannel = new FileInputStream(backupDB).getChannel(); // file channel to manage backup location

        FileInputStream testInput = new FileInputStream(backupDB);

        FileOutputStream out = new FileOutputStream(destinationDB);
        destinationChannel = out.getChannel();

        backupChannel.transferTo(0, backupChannel.size(), destinationChannel);
        backupChannel.close();
        destinationChannel.close();

        Database.getInstance(context).close();
    }catch(IOException e){
        logger.severe("An error as occured. Overwritung DataBase canceled!");
    }
}

非常感谢你的时间! :)

0 个答案:

没有答案