I want to backup a DB, I’ve Google and found code that appears to work, but it ony backs up the DB structure and not the entries in the DB.
There are no errors reported, and also how do you export as CSV format the table?
code for backup a DB:
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();
}
Suggest me where I am lacking?
If you create the backup before you have inserted any data, of course all you get is the structure (as you put it).
Your code copies a complete file, so if there is data in the database, it is getting copied to the card.
I use pretty much the same code (launched from a menu item) to perform backups in several of my apps. I also have code to restore/import, and when I do all my data is there.