当我在 SD 卡上导出数据库时,我使用
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) {
}
结果文件“Income.db”存储在 SD 卡上。
当我从 sd 卡导入新数据库时,我使用
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();
并且数据库没有更新。为什么?我使用 API 7
ps
如果您知道在应用程序中从 sd 卡导入数据库的另一种方法,请告诉,谢谢