views:

1232

answers:

6

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?

+1  A: 

There's a code for exporting the sqlite db to xml on the sd card. Maybe it's good enough?

Drakosha
+3  A: 
Christopher
+1  A: 

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?

sosiouxme
You can comment directly to answers.
fiXedd
On THIS answer - mine - I see an "Add comment" link. Not on the others. Buh?
sosiouxme
yeah 50 rep and you can comment on other peoples answers also. so time to scrounge up 27 more points :-)
Rasmus
A: 

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

A: 

Yes, st.transferFrom(src, 0, src.size()); works fine on my unrooted HTC Hero with android 2.1 update1. Thanks! Adrian

Adrian Aichner
A: 

I don't know what happens if the phone is rooted or not but you should write your files to:

/Android/data/{package_name}/files/

This will work whether it's rooted or not.