I'd like to add a feature to my android app that automatically backs up the sqlite database to the sd card.
What's the best way to go about this? Any examples/tutorials available?
I'd like to add a feature to my android app that automatically backs up the sqlite database to the sd card.
What's the best way to go about this? Any examples/tutorials available?
There's a code for exporting the sqlite db to xml on the sd card. Maybe it's good enough?
My response to the top answer (sorry there doesn't seem to be a way for me to comment directly) would be "Great, so - how do you do THAT?" How do you get the name of the database file, and copy it somewhere on the card?
Hey there,
This code works for me!
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "\\data\\{package name}\\databases\\{database name}";
String backupDBPath = "{database name}";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} catch (Exception e) {}
I was wondering if anyone knows if this will work on non-root phones? I have only tried it on a rooted G1...
Cheers, Neil
Yes, st.transferFrom(src, 0, src.size()); works fine on my unrooted HTC Hero with android 2.1 update1. Thanks! Adrian