2013-05-14 122 views
0

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

請按照[此鏈接](http://stackoverflow.com/a/6542214/2345913) – CRUSADER 2013-05-14 07:14:55

+0

我看過這篇文章。第二個答覆是從哪裏複製我的代碼,第一個答覆只說關於導入不導出 – 2013-05-14 07:22:32

+0

請檢查此鏈接,它可以解決此問題: http://stackoverflow.com/questions/6540906/simple-export- And-import-of-a-sqlite-database-on-android – 2016-01-19 07:55:37

回答

11

SQLite數據庫到我們的本地文件系統 -

函數聲明-

 try { 
      backupDatabase(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

功能defined-

public static void backupDatabase() throws IOException { 
     //Open your local db as the input stream 
     String inFileName = "/data/data/com.myapp.main/databases/MYDB"; 
     File dbFile = new File(inFileName); 
     FileInputStream fis = new FileInputStream(dbFile); 

     String outFileName = Environment.getExternalStorageDirectory()+"/MYDB"; 
     //Open the empty db as the output stream 
     OutputStream output = new FileOutputStream(outFileName); 
     //transfer bytes from the inputfile to the outputfile 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = fis.read(buffer))>0){ 
      output.write(buffer, 0, length); 
     } 
     //Close the streams 
     output.flush(); 
     output.close(); 
     fis.close(); 
    } 
+0

它工作m8非常感謝:)現在導出我應該輸出數據庫文件到這個路徑「/data/data/com.myapp.main/databases/MYDB 「? – 2013-05-14 07:37:03

+0

轉到已安裝的Android路徑'「\ Android \ android-sdk \ platform-tools」'並通過命令提示符運行:'adb pull mnt/sdcard/MYDB',其中MYDB是數據庫的名稱。 – 2013-05-14 07:39:56

+0

我做了它的工作。我重寫了相同的代碼並改變了角色。我從sd讀取數據並寫入數據庫位置並且工作正常。爲了答覆男人和你的幫助 – 2013-05-14 07:47:53

0

以上接受的答案,因爲數據庫的路徑是不同不會對Android版本工作在上面6。

請檢查以下代碼。它將適用於所有設備。

public static boolean exportDB(Context context) { 
    String DATABASE_NAME = "my.db"; 
    String databasePath = context.getDatabasePath(DATABASE_NAME).getPath(); 
    String inFileName = databasePath; 
    try { 
     File dbFile = new File(inFileName); 
     FileInputStream fis = new FileInputStream(dbFile); 

     String outFileName = Environment.getExternalStorageDirectory() + "/" + DATABASE_NAME; 

     OutputStream output = new FileOutputStream(outFileName); 

     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = fis.read(buffer)) > 0) { 
      output.write(buffer, 0, length); 
     } 
     //Close the streams 
     output.flush(); 
     output.close(); 
     fis.close(); 
     return true; 
    } catch (Exception e) { 
     return false; 
    } 
}