当前位置:  开发笔记 > 数据库 > 正文

将SQLite数据库备份并还原到SD卡

如何解决《将SQLite数据库备份并还原到SD卡》经验,为你挑选了3个好方法。

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



1> chrisonline..:

这是我的代码:

    // 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();



2> CommonsWare..:

如何在我的应用程序中自动将数据库备份到SD卡?

使用标准Java I/O复制它.但是请确保您没有任何打开的SQLiteDatabase对象.

然后如何恢复它?

使用标准Java I/O复制它.但是,请确保您没有任何打开SQLiteDatabase旧对象的打开对象.

您可以getPath()SQLiteDatabase对象上使用它来查找它所在的位置,AFAIK(尚未尝试过).



3> Denny Weinbe..:

感谢现有的答案.这是完整的类(使用"getDatabasePath"):

package com.levionsoftware.bills.data.db;

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import com.levionsoftware.bills.MyApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * Created by denny on 16/05/2016.
 * Source: http://stackoverflow.com/questions/18322401/is-it-posible-backup-and-restore-a-database-file-in-android-non-root-devices
 */
public class BackupAndRestore {
    public static void importDB(Context context) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            if (sd.canWrite()) {
                File backupDB = context.getDatabasePath(DBHandler.getDBName());
                String backupDBPath = String.format("%s.bak", DBHandler.getDBName());
                File currentDB = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();

                MyApplication.toastSomething(context, "Import Successful!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void exportDB(Context context) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String backupDBPath = String.format("%s.bak", DBHandler.getDBName());
                File currentDB = context.getDatabasePath(DBHandler.getDBName());
                File backupDB = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();

                MyApplication.toastSomething(context, "Backup Successful!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

推荐阅读
牛尾巴2010
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有