SQLite Forum

Best way check if a file is a SQLite DB?

Best way check if a file is a SQLite DB?

(1) By robnik on 2021-11-06 20:57:48 [link] [source]

Should I...

  1. Try to open it and see if it fails.
  2. Look at the first bytes to see if it's "SQLite format 3", as described here: https://www.sqlite.org/fileformat2.html

I was going to do #2, but maybe older files, or future files, could be different. ??

thanks, Rob

(2.1) By Gerry Snyder (GSnyder) on 2021-11-06 23:09:46 edited from 2.0 in reply to 1 [link] [source]

The first one would open an empty file successfully, which is probably not what you want.

(4) By Simon Slavin (slavin) on 2021-11-06 23:43:40 in reply to 2.1 [source]

Note that using sqlite3_open() doesn't do anything with the file at all. It just sets up a structure in memory. SQLite only looks at the file on storage when you try to do the first thing that needs to know what's in it.

I'd go with your other answer: check the file header to see whether it matches the file-structure documentation.

(3) By Larry Brasfield (larrybr) on 2021-11-06 23:10:06 in reply to 1 [link] [source]

The SQLite 3 database header is stable; it will not change for any major version 3 of the library.

(5) By robnik on 2021-11-07 03:40:11 in reply to 3 [link] [source]

Thanks everyone. I will use the prefix check.