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 に答える