首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将SQLite数据库备份和恢复到SD卡

将SQLite数据库备份和恢复到SD卡
EN

Stack Overflow用户
提问于 2010-01-31 08:10:56
回答 3查看 34.7K关注 0票数 12

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-02-12 20:09:04

下面是我的代码:

代码语言:javascript
运行
复制
    // 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
EN

Stack Overflow用户

发布于 2010-01-31 08:50:28

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

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

以及之后如何恢复它?

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

你可以在SQLiteDatabase对象上使用getPath()来找出它所在的位置,AFAIK (还没有尝试过这个)。

票数 10
EN

Stack Overflow用户

发布于 2016-05-17 02:58:32

多亏了现有的答案。下面是完整的类(使用"getDatabasePath"):

代码语言:javascript
运行
复制
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();
        }
    }
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2170031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档