Possible Duplicate:
Android backup/restore: how to backup an internal database?
我正在开发一个包含 SQL lite db 的应用程序。我需要让用户能够备份数据库并再次恢复它。我怎样才能做到这一点?示例代码?
如果我的用户保存了数据库,然后将应用程序升级到新的数据库版本代码,恢复工作是否可行?我该怎么做?
最佳答案
在文件夹“/data/data/'your.app.package'/databases/”中,您有一个 .db 文件,它是您的数据库。您可以复制该文件,将其保存,然后再放回原处。
如何将数据库备份到外部存储的一个示例:
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 - 在android中备份/恢复sqlite db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13502223/