0

Androidアプリでデータベースを作成し、ステートメントを挿入しました。すべてがうまくいったので、MY_PACKAGE / databses /からデータベースを取得し、SDカードにコピーして到達可能にしたいと思いました。

これは機能しましたが、sqlite Firefoxプラグインで開こうとすると、次のエラーが発生します。

SQLiteManager: Error in opening file Datas.sqlite - either the file is encrypted or corrupt
Exception Name: NS_ERROR_FILE_CORRUPTED
Exception Message: Component returned failure code: 0x8052000b (NS_ERROR_FILE_CORRUPTED) [mozIStorageService.openUnsharedDatabase] 

たぶん私は何か他のもので開く必要がありますか、これを簡単に開くことができませんか?

私が使用したすべてのコードを提供します:

私のデータベースを処理する私はこのすべてのコードを使用しました:

Androidアプリケーションで独自のSQLiteデータベースを使用する

この方法でSDカードにコピーします。

public static boolean backUpDataBase(Context context) {

        final String DATABASE_NAME = "Data.sqlite";
        final String DATABASE_NAME_FULL = "/data/data/package/databases/"
                + DATABASE_NAME;
        boolean result = true;

        // Source path in the application database folder
        String appDbPath = DATABASE_NAME_FULL;

        // Destination Path to the sdcard app folder
        String sdFolder = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + "Datas.sqlite";

        File f = new File(sdFolder);
        // if (!f.exists()) {
        // f.mkdir();
        // }

        InputStream myInput = null;
        OutputStream myOutput = null;
        try {
            // Open your local db as the input stream
            myInput = new FileInputStream(appDbPath);
            // Open the empty db as the output stream
            myOutput = new FileOutputStream(f);

            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
        } catch (IOException e) {
            result = false;
            e.printStackTrace();
        } finally {
            try {
                // Close the streams
                if (myOutput != null) {
                    myOutput.flush();
                    myOutput.close();
                }
                if (myInput != null) {
                    myInput.close();
                }
            } catch (IOException e) {
            }
        }

        return result;
    }

私のデータベースは次のようになります:2つのテーブル:

CREATE TABLE "Test" ("_id" INTEGER PRIMARY KEY  NOT NULL  UNIQUE , "Info" TEXT)

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')

そして、私が必要とするすべてを行うためのコード:

//読み取りと書き込みを行うデータベースを返しますDataBaseHelperdataBase= Main.createOrOpenDB(mContext);

        Main.backUpDataBase(mContext);

        db = dataBase.myDataBase;

        // Step 1: Inflate layout
        setContentView(R.layout.tabs_fragment_activity);

        try{
        db.execSQL("INSERT INTO " +"Test" +" Values ('1','Inserted');");
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

では、挿入は正常に機能するので、どこが間違っているのでしょうか。

4

2 に答える 2

0

SDカードに書き込むコードに問題があるようです(すぐにはわかりません)。

なんでSDCardにコピーするの?ファイルをチェックしたいだけのようですね...

それが実際の目標である場合は、エミュレーターを実行し、単に Eclipse の DDMS ビューを使用してファイルに移動し、右上隅にあるツールチップに「デバイスからファイルをプルする」というボタンをクリックすることをお勧めします。エミュレーターで得られるものは、電話で得られるものとまったく同じでなければなりません。

于 2012-04-23T14:01:39.297 に答える
0

SQLiteOpenHelperを使用してみてください| Android デベロッパー

于 2012-04-23T14:39:51.050 に答える