Where does Android store SQLite's database version?

28,568

Solution 1

You can read the version using android.database.sqlite.SQLiteDatabase.getVersion().

Internally, this method executes the SQL statement "PRAGMA user_version". I got that from the Android source code.

In the database file, the version is stored at byte offset 60 in the database header of the database file, in the field 'user cookie'.

Solution 2

If someone is looking for the java code to read version from file:

private static int getDbVersionFromFile(File file) throws Exception
{
    RandomAccessFile fp = new RandomAccessFile(file,"r");
    fp.seek(60);
    byte[] buff = new byte[4];
    fp.read(buff, 0, 4);
    return ByteBuffer.wrap(buff).getInt();
}
Share:
28,568
bhups
Author by

bhups

Updated on October 18, 2020

Comments

  • bhups
    bhups over 3 years

    I am unable to find where Android stores the database version within the SQLite database file. Where exactly is the database version stored?

  • bhups
    bhups over 13 years
    that was useful, but my question is where does this version info get stored on the filesystem (or may be in database file)?
  • Yaroslav Mytkalyk
    Yaroslav Mytkalyk over 10 years
    And to read and modify it stackoverflow.com/questions/8030779/…
  • xuehui
    xuehui almost 7 years
    how is the database version persisted into the database file?
  • Thomas Mueller
    Thomas Mueller almost 7 years
    @xuehui see the answer: byte 60 in the database file.