繁体   English   中英

将特定的Sqlite数据库还原到Android

[英]Restoring specific Sqlite Database to Android

我可以备份并还原我最近的数据库,但我想保存带时间戳的Sqlite数据库,例如“ back_up_12_11_17_13_32”。 这样,用户不仅可以还原最近的备份,还可以还原他需要的任何备份。

我想知道如何去做?

这是我认为相当灵活的备份/还原工具所使用的一些指针/技术。

我使用以下命令创建备份名称。

private void setFullFilename() {
    backupfullfilename.setText(
            backupbasepart.getText().toString() +
            backupdatetimepart.getText().toString() +
            backupextension.getText().toString()
    );
}

哪里

  • backupbasepart默认为ShopWiseDB
  • backupdatetimepart默认为YYMMDDhhmm格式的当前时间(开始活动时),可通过以下方式获取:

     Calendar cldr = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm"); return "_" + sdf.format(cldr.getTime()); 
  • backupextension默认为.bkp

  • 请注意,我允许所有人进行编辑。

该应用程序的备份/还原活动如下所示:-

在此处输入图片说明

  • 请注意,单击了Spinner以显示可用备份的列表,并还显示如何将时间戳也转换为更易理解的格式。 所有备份都存储在名为ShopWise的子目录的Download目录中的PRIMARY PUBLIC EXTERNAL STORAGE中 (用户可以选择复制/移动到任何位置)(此固定操作是为了简化事务,即不必编写文件浏览器)

使用此目录还可以从其他来源复制数据库,因此用户可以共享数据库(如果愿意),也可以将此功能用于问题确定和解决。

Restore Spinner中显示的备份由3个输入决定,这些输入具有一定的灵活性-默认情况下,仅列出标准备份。 但是,清除基本文件名将得到所有文件(所有扩展名为.bkp的文件(文件扩展名仅显示具有该扩展名的文件,将其清除显示所有扩展名))。

例如,如果所有内容都被清空,那么您可能会有:-

在此处输入图片说明

归根结底,提供可恢复文件列表的核心代码是:-

private void populateRestoreSpinner(Spinner spn,
                                    TextView tv,
                                    String basefilename,
                                    String fileext,
                                    StoreData sd,
                                    TextView restorebutton) {
    int fcount = 0;
    ArrayList<File> reverseflist = new ArrayList<>();

    //
    sd = new StoreData(getResources().getString(R.string.backupdirectoryname),"xxx",true);
    sd.refreshOtherFilesInDirectory();

    // Build the File ArrayList
    ArrayList<File> flst = new ArrayList<>(sd.getFilesInDirectory());

    // Ascertain the relevant files that are needed for the restore backup
    // file selector
    for(int i = 0; i < flst.size(); i++) {
        boolean endingok = flst.get(i).getName().endsWith(fileext);
        boolean containsok = flst.get(i).getName().contains(basefilename);
        if((strictbackupmode && endingok && containsok)
                || (!strictbackupmode && (endingok || containsok))) {
            fcount++;
        } else {
            flst.remove(i);
            i--;
        }
    }

    // Reverse the order of the list so most recent backups appear first
    // Also hide/show the Restore button and spinner according to if
    // files exist or not
    // (doing nothing in the case where the is no restore button i.e.
    //  null has been passed)
    if(flst.size() > 0) {
        for (int i = (flst.size() -1); i >= 0; i--) {
            reverseflist.add(flst.get(i));
        }
        if (restorebutton != null) {
            spn.setVisibility(View.VISIBLE);
            restorebutton.setVisibility(View.VISIBLE);
        }
    } else {
        if (restorebutton != null) {
            spn.setVisibility(View.INVISIBLE);
            restorebutton.setVisibility(View.INVISIBLE);
        }
    }

    // Set the available count for display
    //String bcnt = "Available Backups=" + Integer.toString(reverseflist.size());
    tv.setText(Integer.toString(reverseflist.size()));

    // Set the spinner adapter and dropdown layout and then set the
    // spinner's adapter
    AdapterFileList afl = new AdapterFileList(this,
            R.layout.filelist,
            reverseflist,
            getIntent());
    afl.setDropDownViewResource(R.layout.filelist);
    spn.setAdapter(afl);
}

注意! StoreData是一个美化的文件/目录列表,虽然列出了很长一段时间,但是基本上具有诸如以下的成员:

private String directory;
private String subdirectory;
private String filename;
private boolean mounted;
private boolean inerror;
private boolean fileexists;
private boolean direxists;
private long errorcode;
private ArrayList<String> errorlist = new ArrayList<>();
private ArrayList<File> otherfilesindirectory = new ArrayList<>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM