1

我看过一些关于如何在 android 中导入和导出数据库的帖子,我找到了这些代码,但我似乎无法让它工作。我收到错误 java.io.filenotfoundexception /storage/sdcard0/BackupFolder/DatabaseName:open failed ENOENT (no such file or directory)。我改变了一些东西,但我仍然没有找到文件异常

这是我的出口:

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();
    }
}
4

2 回答 2

12

SQlite 数据库到我们的本地文件系统-

函数声明-

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

功能定义——

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();
    }
于 2013-05-14T07:18:12.427 回答
0

以上接受的答案不适用于 6 或以上的 Android 版本,因为数据库路径不同。

请检查下面的代码。它适用于所有设备。

    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;
    }
}
于 2017-03-06T11:06:27.583 回答