可能重复:
Android备份/恢复:如何备份内部数据库?
我正在开发一个有SQL lite db的应用程序.我需要让用户能够备份数据库并再次恢复它.我怎样才能做到这一点?示例代码?
如果我的用户保存了数据库,然后将应用程序升级到新的数据库版本代码,还原工作?我该怎么做?
Ped*_*pes 40
在"/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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48269 次 |
| 最近记录: |