0

I'm copying a file with the structure of my SQLite database from the assets into my application folder when onCreate(SQLiteDatabase database) is called in the SQLiteOpenHelper class, but the original file and the copied one are not exactly equals:

winmerge capture

(Original file from the assets folder at the right, copied one at the left)

As you can see, only the first block is different, and the rest of the file is exactly the same.

My onCreate() method is:

@Override
public void onCreate(SQLiteDatabase arg0) {
    Log.i(TAG, "onCreate");
    try {
        copyDatabase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And the copyDatabase() method is:

private void copyDatabase() throws IOException {
    InputStream input = context.getAssets().open(DB_NAME);
    String outFilename = DB_PATH;
    OutputStream output = new FileOutputStream(outFilename);
    byte[] buffer = new byte[1024];
    int mLength;
    while ((mLength = input.read(buffer)) > 0) {
        output.write(buffer, 0, mLength);
    }
    output.flush();
    output.close();
    input.close();
}

I have no idea why my copyDatabase() method is not working properly in, I guess, in the first iteration of the loop.

Any idea?

moictab
  • 959
  • 6
  • 27
  • What are the values of DB_NAME and DB_PATH? What are the file sizes? – greenapps May 18 '15 at 16:23
  • DB_NAME is a string with the name of the database, and DB_PATH is a string with the path of the database file: `DB_PATH = context.getDatabasePath(DB_NAME).getPath();` – moictab May 18 '15 at 16:26
  • And the files size is exactly the same for both files, 82.944 bytes – moictab May 18 '15 at 16:27
  • `As you can see, only the first block is different, and the rest of the file is exactly the same.`. ???? Sorry. Dont see that. What kind of program is displaying this? – greenapps May 18 '15 at 16:29
  • You should really tell the values. Starting with DB_NAME. – greenapps May 18 '15 at 16:33
  • Is winmerge, in the two columns showed on the left side of the screen you can see, in the upper zone, that there is gray and yellow areas, while the rest of the columns are white. White color means that both files are exactly the same, while gray and yellow means that there are differences. – moictab May 18 '15 at 16:34
  • `SQLiteOpenHelper class, `, Dont use that class. You are copying the file yourself. Just copy it in onCreate of an activity or so. – greenapps May 18 '15 at 16:36
  • How does winmerge get the copied file from internal private memory? – greenapps May 18 '15 at 16:40
  • Make hexdumps of both files, and compare those. – CL. May 18 '15 at 16:50

0 Answers0