【问题标题】:Backup and restore SQLite database to sdcard备份和恢复 SQLite 数据库到 SD 卡
【发布时间】:2011-01-11 07:39:26
【问题描述】:

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

【问题讨论】:

    标签: android sqlite backup


    【解决方案1】:

    这是我的代码:

        // 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】:

      如何将我的数据库备份到 sdcard 自动在我的应用中?

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

      然后我该如何恢复它?

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • getExternalStorageDirectory() 已弃用任何新的解决方案?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        相关资源
        最近更新 更多