0

アプリとそのデータベースを更新することにしましたが、以下のコードを実行してアプリを実行すると、データベースが奇妙な方法でコピーされます。一部の部分はコピーされ、一部はコピーされません。

私を最も混乱させているのは、Nexusで問題が発生している間、すべてがエミュレーターで完全に機能することです。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {
        copyDatabase();
    } catch (IOException e) {
        throw new Error("Error copying database" + e.toString());
    }
}

public void copyDatabase() throws IOException {
    // Open your local db as the input stream
    InputStream myinput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outfilename = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myoutput = new FileOutputStream(outfilename);

    // transfer byte to inputfile to outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myinput.read(buffer)) > 0) {
        myoutput.write(buffer, 0, length);
    }

    // Close the streams
    myoutput.flush();
    myoutput.close();
    myinput.close();
}
4

2 に答える 2

0

オフセットは常に 0 です。

そのため、常に最初の 1024 バイトをコピーしています。myinput

于 2013-06-18T21:52:06.420 に答える
0

確かに、この方法ではなく、最初に一時的な場所に保存して、SELECT * を実行し、それらを新しいデータベースにダンプする方が良いでしょう。テーブル スキーマを変更するとどうなりますか?

また、これはファイルを開いたり上書きしたりしていませんか?outfilenameDB パスと同じように見えます。

于 2013-06-18T21:50:38.573 に答える