我正在尝试导入/导出我的SQLite数据库,但导出正在运行。我将database.db
导出到外部存储,但是当我导入时,我没有收到任何错误,但导入的记录没有得到反映。
这是我的代码。
public void importDatabase() throws IOException {
if(isExternalStorageGranted()){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Import Database");
alert.setMessage("do you want to replace database?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
RemindersDbAdapter objRemindersDbAdapter = new RemindersDbAdapter(ImportExportActivity.this);
objRemindersDbAdapter.open();
String oldDBPath = Environment.getExternalStorageDirectory().getPath() + separator + Constants.APP_FOLDER
+ separator + SAMPLE_DB_NAME+".db";
String currentDBPath = "/data/user/0/" + getPackageName() + "/databases/" + SAMPLE_DB_NAME;
// database to internal oldDBPath.
File newDb = new File(oldDBPath);
File oldDb = new File(currentDBPath);
copyFile(new FileInputStream(oldDb), new FileOutputStream(newDb));
objRemindersDbAdapter.close();
} catch (IOException e) {
Toast.makeText(ImportExportActivity.this, "Failed to import database", Toast.LENGTH_SHORT).show();
}
}
});
alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.create().show();
}
}
private void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
fromChannel = fromFile.getChannel();
toChannel = toFile.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
Toast.makeText(this, "Database Imported Successfully", Toast.LENGTH_SHORT).show();
} finally {
try {
if (fromChannel != null) {
fromChannel.close();
}
} finally {
if (toChannel != null) {
toChannel.close();
}
}
}
}
private void exportDB() {
if(isExternalStorageGranted()){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source = null;
FileChannel destination = null;
String currentDBPath = "/data/" + getApplicationContext().getPackageName() + "/databases/" + SAMPLE_DB_NAME;
String backupDBPath = SAMPLE_DB_NAME + ".db";
File currentDB = new File(data, currentDBPath);
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+ Constants.APP_FOLDER);
if(!directory.exists())
directory.mkdirs();
File backupDB = new File(directory, backupDBPath);
LogFile.appendLog("backupDB DB Path : -" + backupDB.getPath());
if (backupDB.exists()) {
boolean isPrevfileDelete = backupDB.delete();
}
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我没有收到任何错误,但导入导入的记录后没有反映在sqlite数据库中。
答案 0 :(得分:0)
当有任何打开的连接时,您不能访问数据库文件。
确保在执行任何文件操作之前关闭所有连接。