为离线应用程序创建备份的最佳方法? [英] Room best ways to create backups for offline application?

查看:83
本文介绍了为离线应用程序创建备份的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想说一个非常复杂的数据库,它使用many to many数据库设计和foreign keys并联接表,它是Room数据库.我想为其创建备份系统,因为它是脱机应用程序,因此我需要导出数据库并将其存储到Google Drive的应用程序文件夹中.我最近几天读了很多关于它的文章,但是仍然有些困惑,我看到有一些方法可以将数据库导出为 CSV JSON excel strong>文件或与.db文件一样,但是这样做的最佳实践是什么以及专业人员如何处理?

So I have let's say pretty complex database which is using many to many database design with foreign keys and join tables and it's Room database. I want to create backup system for it because it's offline application I would need to export database and store it to google drive's app folder. I have read quite a lot about it in recent day's but still a bit confused I saw that there is ways to export database as CSV, JSON, excel files or just as .db file, but what are the best practices for doing so and how professionals dealing with it?

推荐答案

几乎不需要做任何复杂的事情,只需保存SQLiteDatabase文件即可.

There is very little need to do anything complex, rather simply save the SQLiteDatabase file.

基本上关闭Room db,然后保存文件.

Basically close Room db then save the file.

例如以下是一个非常基本的示例,该示例保存在一个名为DBsaves的子目录中的downloads目录中:-

e.g. the following is a very rudimentary example that saves to the downloads directory in a sub-directory called DBsaves :-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    resetSequenceAction();
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        verifyStoragePermissions(this);
    }
}

@Override
protected void onStart() {
    super.onStart();
    mTestDB = Room.databaseBuilder(this,TestDatabase.class,TestDatabase.DBNAME).build();
    addSomeData();
    addSomeData();
    addSomeData();
    addSomeData();
    mTestDB.close();
    File dbfile = this.getDatabasePath(TestDatabase.DBNAME);
    File sdir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"DBsaves");
    String sfpath = sdir.getPath() + File.separator + "DBsave" + String.valueOf(System.currentTimeMillis());
    if (!sdir.exists()) {
        sdir.mkdirs();
    }
    File savefile = new File(sfpath);
    try {
        savefile.createNewFile();
        int buffersize = 8 * 1024;
        byte[] buffer = new byte[buffersize];
        int bytes_read = buffersize;
        OutputStream savedb = new FileOutputStream(sfpath);
        InputStream indb = new FileInputStream(dbfile);
        while ((bytes_read = indb.read(buffer,0,buffersize)) > 0) {
            savedb.write(buffer,0,bytes_read);
        }
        savedb.flush();
        indb.close();
        savedb.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}


public void verifyStoragePermissions(Activity activity) {

    final int REQUEST_EXTERNAL_STORAGE = 1;
    String[] PERMISSIONS_STORAGE = {

            //Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    int permission = ActivityCompat.checkSelfPermission(
            activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if(permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

  • 请注意仅包含了onCreate和verifyStoragePermissions方法以获取写入外部存储的权限(请注意,用户权限也在清单中设置).

    • Note the the onCreate and verifyStoragePermissions methods only included to get the permission to write to external storage (note user permissions also set in manifest).

      • 导入操作是在Room外部执行此操作(如果要从备份还原,也是如此).

      运行后:-

      然后将文件复制到PC并使用SQLite Manager打开:-

      And then copying the file to a PC and opening with SQLite Manager :-

      这完全符合预期并且具有高度的可移植性,即您可以将其放入任何SQLite工具中(该工具使用的SQLite版本可能是一个限制因素)

      This being entirely as expected and as shown highly portable i.e. you can drop it into any SQLite tool (SQLite version used by such a tool may be a restrictive factor)

      这篇关于为离线应用程序创建备份的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆