SQLite Forum

compare two databases
Login

compare two databases

(1) By anonymous on 2022-05-11 07:17:26 [link] [source]

Could you please teach us the function of comparing whether two databases are consistent?

Background: I have a backup database, if it is consistent with the main database, I will not backup.

1、By comparing the md5 of the two databases 2、Compare the contents of the database sentence by sentence?

(2) By ddevienne on 2022-05-11 07:40:12 in reply to 1 [link] [source]

There are two official1 existing utilities that approximate your goals.

comparing the md5 of the two databases

https://www.sqlite.org/dbhash.html

Compare the contents of the database

https://www.sqlite.org/sqldiff.html


  1. ^ I.e. from the SQLite developers themselves, mostly Richard.

(3) By Gunter Hick (gunter_hick) on 2022-05-11 07:47:32 in reply to 1 [source]

If your "database file" and "backup file" are both recognizable as an SQLite databse, have the same file size and the same md5sum, then it is highly unlikely that their contents will be different.

OTOH if your perform a VACUUM operation on a database file after significant activity (insert, update, delete), then the internal structure will change, without affecting the information content.

There are a couple of tricks to speed up the determination without going through the whole contents of both databases (which may well be more effort than just creating a backup anyway). There are two version numbers available in the database header: "Schema version" and "user version".

The schema version is updated by SQLite every time the schema (metadata) changes. This is used to determine if a prepared statement requires re-preparing. If they differ between main and backup databases, then this would fit most people's defintion of inconsistent.

The user version is maintained by the user, i.e. your application. Any change that violates your definition of "consistent" needs to change the user version, so that a quick check can be used to determine the presence of absence of changes.

Does adding, changing or removing an index violate your definition of "consistent"? It does not change the "information" inside the database; only the ease of retrieving it.