首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将特定Sqlite数据库恢复到Android

将特定Sqlite数据库恢复到Android
EN

Stack Overflow用户
提问于 2017-11-12 21:35:47
回答 1查看 40关注 0票数 0

我可以备份和恢复我最近的数据库,但我想保存一个带有时间戳的Sqlite数据库,比如"back_up_12_11_17_13_32“。这样,用户不仅可以恢复最近的备份,还可以恢复他需要的任何备份。

我想知道该怎么做?

EN

回答 1

Stack Overflow用户

发布于 2017-11-13 09:46:03

这里有一些我认为是相当灵活的备份/恢复工具的指针/技术。

我使用以下代码来创建备份名称。

代码语言:javascript
复制
private void setFullFilename() {
    backupfullfilename.setText(
            backupbasepart.getText().toString() +
            backupdatetimepart.getText().toString() +
            backupextension.getText().toString()
    );
}

哪里

  • backupbasepart默认为ShopWiseDB
  • backupdatetimepart默认为当前时间(活动启动时),格式为YYMMDDhhmm,通过以下方式获得:-

sdf.format(cldr.getTime());

  • backupextension cldr = .bkp

  • Noting ();Calendar.getInstance sdf = SimpleDateFormat SimpleDateFormat("yyyyMMddHHmm");return "_“+ Calendar默认为我允许编辑所有内容。

应用程序的备份/恢复活动如下所示:

  • 请注意,单击微调器可以显示可用备份的列表,还可以显示时间戳如何转换为更易于阅读的格式。所有备份都存储在主公共外部存储(用户可以选择复制/移动到任何位置)中的下载目录中名为ShopWise的子目录中(修复此问题是为了简化问题,即不必编写文件资源管理器)

使用此目录还允许从其他来源复制数据库,因此用户可以根据需要共享数据库,或者此功能可用于确定和解决问题。

Restore微调器中显示的备份由允许一定灵活性的3个输入确定-默认情况下,仅列出标准备份。但是,清除基本文件名将导致所有文件(以及所有扩展名为.bkp的文件(文件扩展名仅显示具有该扩展名的文件,清除它将显示所有扩展名))。

例如,如果所有内容都被删除,那么您可以拥有:-

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

代码语言:javascript
复制
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是一个美化的文件/目录列表,它相当冗长,但基本上包含以下成员:-

代码语言:javascript
复制
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<>();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47249433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档