将SQLite数据库备份并恢复到SD卡上

12

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

3个回答

11

这是我的代码:

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

11
如何在我的应用中自动将数据库备份到SD卡中? 使用标准的Java I/O进行复制。但确保没有打开任何SQLiteDatabase对象。
接下来我该如何恢复数据库呢? 同样使用标准的Java I/O进行复制。但确保没有打开任何旧数据库的SQLiteDatabase对象。
据我所知,您可以在SQLiteDatabase对象上使用getPath()方法查找其所在位置(尚未尝试过)。

点赞。谢谢你解决了我的问题,我试了很多个小时来备份和恢复我的数据库,问题是我忘记关闭数据库连接了。 - Omer123

9

感谢已有的答案。以下是完整的类(使用“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: https://dev59.com/KnXYa4cB1Zd3GeqP_N1H
 */
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();
        }
    }
}

getExternalStorageDirectory()已经过时了,有新的解决方案吗? - Zakaria Darwish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接