2013-05-14 124 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; 
    } 
}