如何将SQLite数据库备份到SD卡?

时间:2018-08-19 08:16:51

标签: android sqlite

如何将SQLite数据库备份到SD卡?我在这里查看了许多示例,但没有一个对我有用。我在清单中添加了以下几行:

static func sendPostDataToDatabase ( title: String?,
                                   content: String?,
                                   onSuccess: @escaping () -> Void,
                                   onError: @escaping (_ errorMessage: String?) -> Void) {

    let newPostId = Api.Post.REF_POSTS.childByAutoId().key  // THIS creates the new ID of a post, it's just a dynamic way to get a unique post ID , you can also use  NSUUID().uuidString which will give you a unique string

    // the definition of `uuidString` is: A string containing a formatted UUID for example E621E1F8-C36C-495A-93FC-0C247A3E6E5F. 

    let newPostReference = Api.Post.REF_POSTS.child(newPostId) 

    // MARK: - TIMESTAMP FEATURE
    let timestamp = Int(Date().timeIntervalSince1970)

    var newPostDict = [    "userUid"      : Api.Users.CURRENT_USER!.uid,
                           "title"        : title as Any,
                           "content"      : content as Any,
                           "timestamp"    : timestamp
                        ] as [String : Any]

      newPostReference.setValue(newPostDict) { (error, databaseRef) in
        if error != nil {
            onError(error!.localizedDescription)
            return
        }
        else {
            onSuccess(newShipId, photosArray[0])
        }
    }

,然后将其复制到带有textview的片段,以单击并执行备份:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

和以下方法:

    // Find the Text View that displays the Backup Database Button //
    TextView backupDatabaseTextView = (TextView) rootview.findViewById(R.id.backup_database_text_view);

    // Set a click listener on the backup database text view //
    backupDatabaseTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            exportDB();

        }
    });

1 个答案:

答案 0 :(得分:0)

  

当我调试时,它会找到sd卡,但是会掉到try catch的底部

我不知道该怎么解释,因为我不知道您认为该代码中的哪几行代表“查找sd卡”。例如,您的代码与可移动存储无关。

所以,这是各种问题,按照可能从高到低的顺序排列(尽管您应该修复所有问题):

不支持运行时权限

您正在Android 8.1上运行。从Android 6.0开始,您需要request certain permissions at runtime,并且WRITE_EXTERNAL_STORAGE属于这些权限。

硬编码的数据库路径

String currentDBPath = "//data//" + "com.budgettrackpro.android.budgettrackpro"
                + "//databases//" + "budgettrackpro.db";

从不硬编码路径。替换为:

File currentDB = getActivity().getDatabasePath("budgettrackpro.db");

(并摆脱File currentDB = new File(data, currentDBPath);行,因为您将不再需要它)

不记录异常

在开发应用程序时,记录所有例外情况。将此行添加到Toast.makeText()块中的catch行上方:

Log.e(getClass().getSimpleName(), "Exception backing up database", e);

然后,use LogCat检查与任何崩溃相关的堆栈跟踪。

在主应用程序线程上执行I / O

充其量,您的用户界面将在exportDB()调用期间被冻结,并且用户会认为您的应用程序已损坏。最坏的情况是,您将崩溃,因为:

  • 备份时间太长,占用了主应用程序线程,或者
  • StrictMode抱怨主应用程序线程上的磁盘I / O

请将此工作移至后台线程。

不向用户显示备份

即使创建了文件,the user will not be able to see that it exists也无需重启或等待几个小时。

相关问题