【问题标题】:how to restore the database backup of sqlite in andorid for non rooted device如何在android中为非root设备恢复sqlite的数据库备份
【发布时间】:2017-01-17 08:32:51
【问题描述】:

我正在尝试将 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();

            }
        }
        });

点击休息按钮后,这是安卓监视器的输出:

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

【问题讨论】:

    标签: android sqlite android-sqlite


    【解决方案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 是通过微调器从可用备份列表中选择的(略有不同的是,备份是在公共外部存储中)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多