我見過一些關於如何導入和導出數據庫在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