When I export a database on sd card I use
try {
File sd = new File(Environment.getExternalStorageDirectory()+"/ABC");
File data = Environment.getDataDirectory();
boolean success = false;
if (!sd.exists()) {
success = sd.mkdir();
}
if (sd.canWrite()) {
String currentDBPath = "\\data\\com.example.skurat\\databases\\Income.db";
String backupDBPath = "Income.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists() && success) {
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) {
}
as a result file “Income.db” is stored on the sd card.
When I import a new database from sd card I use
File dbfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/ABC/Income.db" );
String backupDBPath = "Income.db";
File backupDB = new File(dbfile, backupDBPath);
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
// toDoDBAdapter = new DBAdapter(this, "Income.db"); written in the "onCreate" method, working correctly
toDoDBAdapter.dbHelper.onUpgrade(db, 1, 2);
// refresh screen, working correctly
populateTodoList();
And database is not updated. Why? I use API 7
p.s.
If you know another way to import the database from sd card in the application, please tell, thanks
There is no code that imports the database. Your code just calls
onUpgradeon the database on the SD card.You have to copy the database file back, like you do for export. Alternatively, you have to copy the records from that database into your normal database.