try {
final String inFileName = "/data/data/your app package/databases/db";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CALC/Backup";
File dir = new File(path);
if (!dir.exists()) dir.mkdirs();
String outFileName = path + "/filename"; // output file name
// 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);
}
Toast.makeText(getActivity(), "Backup Successfully", 2).show();
// Close the streams
output.flush();
output.close();
fis.close();
}
catch (Exception e) {
e.printStackTrace();
}
3 回答
sqlite3命令行工具具有.backup dot command .
您可以使用以下命令连接到数据库:
并使用以下命令运行backup dot命令:
您也可以进行备份,然后关闭连接,而不是与数据库的交互连接
无论哪种方式,结果都是数据库
my_database.sq3
的名为backup_file.sq3
的副本 .它与定期文件复制不同,因为它会处理当前正在处理数据库的任何用户 . 在数据库上设置了正确的锁,因此备份是专门完成的 .
.backup是最好的方法 .
您还可以尝试.dump命令,它使您能够将整个数据库或表转储到文本文件中 . 如果指定了TABLE,则仅转储与LIKE模式TABLE匹配的表 .