导入/导出到android sqlite数据库

时间:2013-05-14 07:12:30

标签: 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();
    }
}

2 个答案:

答案 0 :(得分: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();
    }

答案 1 :(得分: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;
    }
}