我见过一些关于如何导入和导出数据库在Android的帖子,我发现这些代码,但我似乎无法使它的工作。我得到错误java.io.filenotfoundexception/storage/sdcard0/BackupFolder/DatabaseName:打开失败的ENOENT(没有这样的文件或目录)。香港专业教育学院改变了一些东西,但我仍然没有得到文件中发现异常导入/导出到Android的SQLite数据库
这里是我的出口:
private void exportDB() {
try {
db.open();
File newFile = new File("/sdcard/myexport");
InputStream input = new FileInputStream(
"/data/data/com.example.mycarfuel/data
bases/MyDatabase");
OutputStream output = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
db.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
和我的导入:
private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "PackageName"
+ "//databases//" + "DatabaseName";
String backupDBPath = "/BackupFolder/DatabaseName
";
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(getBaseContext(), backupDB.toString(),
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
}
请按照[此链接](http://stackoverflow.com/a/6542214/2345913) – CRUSADER 2013-05-14 07:14:55
我看过这篇文章。第二个答复是从哪里复制我的代码,第一个答复只说关于导入不导出 – 2013-05-14 07:22:32
请检查此链接,它可以解决此问题: http://stackoverflow.com/questions/6540906/simple-export- And-import-of-a-sqlite-database-on-android – 2016-01-19 07:55:37