我想备份一个数据库,我在谷歌上找到了似乎可以工作的代码,但它只备份了数据库结构,而不是数据库中的条目。
没有报告错误,以及如何将表格导出为 CSV 格式?
备份数据库的代码:
public void BackupDB() {
InputStream myInput;
try {
myInput = new FileInputStream("/data/data/com.cecchina.mathew.lessonsDB/databases/Teaching");
// Set the output folder on the SDcard
File directory = new File("/mnt/sdcard/external_sd/TeachingBKup/");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
// Set the output file stream up:
OutputStream myOutput = new FileOutputStream(directory.getPath()+ "/Teaching.backup");
// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (FileNotFoundException e) {
Toast.makeText(ourContext, "Backup Unsuccesfull file not found error!", Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(ourContext, "Backup Unsuccesfull IO error!", Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(ourContext, "DataBase structure backed up!", Toast.LENGTH_LONG).show();
}
建议我哪里欠缺?