问题描述
可能的重复:
Android 备份/恢复:如何备份内部数据库?一个>
我正在开发一个包含 SQL lite db 的应用程序.我需要让用户能够备份数据库并再次恢复它.我怎样才能做到这一点?示例代码?
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?
如果我的用户保存了数据库,然后将应用程序升级到新的数据库版本代码,恢复是否有效?我该怎么做?
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 db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!