The solution below has been extracted from a response in SOEN and does what you want, although there is plenty of room for improvement and better portability:
Import
private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "<nome do package>"
+ "//databases//" + "<nome da BD>";
String backupDBPath = "<nome ficheiro backup da BD>"; // No SDCard
File backupDB = new File(data, currentDBPath);
File currentDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Importação com sucesso!",
Toast.LENGTH_SHORT).show();
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Importação Falhou!", Toast.LENGTH_SHORT)
.show();
}
}
Export
private void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "<nome do package>"
+ "//databases//" + "<nome da BD>";
String backupDBPath = "<destino>";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Backup com sucesso!",
Toast.LENGTH_SHORT).show();
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Backup Falhou!", Toast.LENGTH_SHORT)
.show();
}
}
User response credits @adefran83 in this answer in the SOEN.
Not a duplicate of http://answall.com/questions/33387/comorcopiar-banco-sqlite-da-memoria-interna-para-o-cart%C3%A3o-sd-comando-linux/33481#33481?
– Wakim
I need to backup for everyone who has the application installed!
– César Alves
Ah, it wasn’t clear from the question. You can use the
BackupHelper
in that case: http://developer.android.com/reference/android/app/backup/BackupHelper.html– Wakim
doubt of it I believe is how to export the file and how to import.
– Joannis
You have a good coded help with this post http://www.techrepublic.com/blog/software-engineer/export-sqlite-data-from-your-android-device/
– Tony
how it wants to save the bank and transfer to another cell phone, without it exporting to a file and importing? Besides that it did not make clear
– Joannis
would be just that, simply export to SD or anything else, and then import again so the application can load!
– César Alves
I have the same problem but reissued the question, so I created a slightly different one http://answall.com/questions/38422/exportar-arquivos-do-sqlite-para-csv @Check it out and see if anything helps you
– Joannis