在Android的备份/恢复的SQLite数据库 [英] Backup/restore sqlite db in android

查看:742
本文介绍了在Android的备份/恢复的SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/5282936/android-backup-restore-how-to-backup-an-internal-database">Android备份/恢复?如何备份内部数据库

我开发一个应用程序,其中有SQL精​​简版数据库。我需要给用户备份数据库,然后再恢复它的能力。我该怎么办呢?样品code?

I am developing an app in which there is SQL lite db. I need to give the user the ability to back up the database and restore it again. How can I do that? Sample code?

和的情况下,我的用户保存的数据库,然后将应用程序升级到新的数据库版本code,将恢复工作?我怎么去的?

And in case my user saved the db, and then upgraded the app to a new DB version code, will restore work? How do I go about that?

请帮忙

推荐答案

在文件夹/data/data/'your.app.package'/databases/你有一个.db文件是数据库。您可以复制该文件,保存它,然后再次回到那里把它。

In the folder "/data/data/'your.app.package'/databases/" you have a .db file that is your database. You can copy that file, save it, and then place it back there again.

这是一个例子,如何备份数据库到外部存储:

One example on how to backup the database to the external storage:

    final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);

    String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";

    // Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer))>0){
        output.write(buffer, 0, length);
    }

    // Close the streams
    output.flush();
    output.close();
    fis.close();

这篇关于在Android的备份/恢复的SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆