如何在非rooted设备的andorid中恢复sqlite的数据库备份

时间:2017-01-17 08:32:51

标签: android sqlite android-sqlite

我正在尝试将bike_info.db恢复到我的应用程序 db文件有一些记录,我想在应用程序中恢复它们

我尝试使用下面的代码,显示toast成功但db没有恢复,我不知道为什么。有人可以帮忙吗?

rest = (Button) findViewById(R.id.cview_restore);

    rest.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                File sd = Environment.getExternalStorageDirectory();
                File data = Environment.getDataDirectory();
                if (sd.canWrite()) {

                    String currentDBPath = "//data//" + "com.infyco.kp.new_tab"
                            + "//databases//" + "bike_info.db";
                    String backupDBPath = "//data//bike_info.db"; // From SD directory.
                    File backupDB = new File(data, currentDBPath);
                    File currentDB = new File(sd, backupDBPath);

                    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(), "Import Successful!",
                            Toast.LENGTH_SHORT).show();

                }
            } catch (Exception e) {

                Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
                        .show();

            }
        }
        });

点击“休息”按钮后,这是android monitor的输出:

01-17 14:00:40.922 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-17 14:00:40.962 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: ViewPostImeInputStage processPointer 1

01-17 14:00:41.012 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: #1 mView = android.widget.LinearLayout{9ba3ae9 V.E...... ......I. 0,0-0,0 #102039d android:id/toast_layout_root}

01-17 14:00:41.072 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1

01-17 14:00:43.022 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: #3 mView = null

1 个答案:

答案 0 :(得分:1)

以下是工作恢复的核心还原代码。一些差异: 我在关闭前冲洗。

我重命名原始数据库(复制并删除原始数据库),因此它不存在(如果还原失败,则很容易还原)。

我也在它自己的线程中做(这里没有显示)。

我从系统中获取数据库的路径/文件名。

                dbfile = new File(currentdbfilename);    
                .......             
                try {
                    // Stage 1 Create a copy of the database
                    Log.i(logtag, "Stage 1 (make Copy of current DB)Starting");
                    FileInputStream fis = new FileInputStream(dbfile);
                    OutputStream backup = new FileOutputStream(copydbfilename);
                    while ((copylength = fis.read(buffer)) > 0) {
                        backup.write(buffer, 0, copylength);
                    }
                    backup.flush();
                    backup.close();
                    fis.close();
                    Log.i(logtag, "Stage 1 - Complete. Copy made of current DB.");
                    copytaken = true;

                    // Stage 2 - Delete the database file
                    if (dbfile.delete()) {
                        Log.i(logtag, "Stage 2 - Completed. Original DB deleted.");
                        origdeleted = true;
                    }

                    // Stage 3 copy from the backup to the deleted database file i.e. create it
                    Log.i(logtag, "Stage 3 - (Create new DB from backup) Starting.");
                    FileInputStream bkp = new FileInputStream(backupfilename);
                    OutputStream restore = new FileOutputStream(currentdbfilename);
                    copylength = 0;
                    while ((copylength = bkp.read(buffer)) > 0) {
                        restore.write(buffer, 0, copylength);
                    }
                    Log.i(logtag, "Stage 3 - Data Written");
                    restore.flush();
                    restore.close();
                    Log.i(logtag, "Stage 3 - New DB file flushed and closed");
                    restoredone = true;
                    bkp.close();
                    Log.i(logtag, "Stage 3 - Complete.");
                } catch (IOException e) {
                    e.printStackTrace();
                    if(!copytaken) {
                        errlist.add("Restore failed copying current database. Error was " + e.getMessage());
                    } else {
                        if(!origdeleted) {
                            errlist.add("Restore failed to delete current database. Error was " + e.getMessage());
                        }
                        else {
                            if(!restoredone) {
                                errlist.add("Restore failed to recreate the database from the backup. Error was "+ e.getMessage());
                                errlist.add("Restore will attempt to revert to the original database.");
                            }
                        }
                    }
                }
使用以下命令设置

currentdbfilename : -

            currentdbfilename = this.getDatabasePath(
                DBConstants.DATABASE_NAME)
                .getPath();
通过微调器从可用备份列表中选择

copydbfilename (稍有不同的是备份在公共外部存储中)。