备份和恢复 SQLite 数据库到 sdcard [英] Backup and restore SQLite database to sdcard

查看:50
本文介绍了备份和恢复 SQLite 数据库到 sdcard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的应用程序中自动将我的数据库备份到 SD 卡?之后,我如何恢复它?

How can I backup my database to the sdcard automatically in my app? And afterward, how do I restore it?

推荐答案

这是我的代码:

    // Local database
    InputStream input = new FileInputStream(from);

    // create directory for backup
    File dir = new File(DB_BACKUP_PATH);
    dir.mkdir();

    // Path to the external backup
    OutputStream output = new FileOutputStream(to);

    // transfer bytes from the Input File to the Output File
    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer))>0) {
        output.write(buffer, 0, length);
    }

    output.flush();
    output.close();
    input.close();

这篇关于备份和恢复 SQLite 数据库到 sdcard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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