Android代码导出数据库,导入/导出到android sqlite数据库
我看過幾篇關于如何在android中導入和導出數據庫的帖子,我找到了這些代碼,但我似乎無法使它工作.我收到錯誤java.io.filenotfoundexception / storage / sdcard0 / BackupFolder / DatabaseName:打開失敗的ENOENT(沒有這樣的文件或目錄).我改變了一些東西,但我仍然沒有找到文件異常
這是我的出口:
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();
}
}
總結
以上是生活随笔為你收集整理的Android代码导出数据库,导入/导出到android sqlite数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html 自动排序代码,SEO中常用HT
- 下一篇: echart 三维可视化地图_揭秘720