Android 备份和恢复 SQLite 数据库到 sdcard

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2170031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:08  来源:igfitidea点击:

Backup and restore SQLite database to sdcard

androidsqlitebackup

提问by chrisonline

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

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

采纳答案by chrisonline

Here is my code:

这是我的代码:

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

回答by CommonsWare

How can i backup my database to the sdcard automatically in my app?

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

Copy it using standard Java I/O. Make sure you don't have any open SQLiteDatabaseobjects, though.

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

And afterwards how do i restore it?

之后我如何恢复它?

Copy it using standard Java I/O. Make sure you don't have any open SQLiteDatabaseobjects to the old database, though.

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

You can use getPath()on a SQLiteDatabaseobject to find out where it resides, AFAIK (haven't tried this).

您可以使用getPath()一个上SQLiteDatabase对象来找出它驻留,据我所知(没有试过)。

回答by Denny Weinberg

Thanks to the existing answers. Here is the complete class (with usage of "getDatabasePath"):

感谢现有的答案。这是完整的类(使用“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();
        }
    }
}