On Thu, 19 Sep 2019 at 16:03, Dominique Devienne <ddevie...@gmail.com>
wrote:

> On Wed, Sep 18, 2019 at 6:43 PM Clemens Ladisch <clem...@ladisch.de>
> wrote:
>
> > Peng Yu wrote:
> > > Is there a better way to just return an exit status of 0 for
> > > a sqlite3 DB file and 1 otherwise?
> >
> > Extract the magic header string from a known DB file:
> >
> >   dd bs=16 count=1 < some.db > sqlite3-signature
> >
> > Then you can compare it against the beginning of the file:
> >
> >   cmp --bytes=16 sqlite3-signature /tmp/tmp.erZ5aS6PUX.sqa > /dev/null
> >   [ $? = 0 ] && echo SQLite DB
>
>
> I'm actually surprised sqlite3[.exe] itself doesn't have a more to do that.
> I tried using it to open a non-DB file, and it opens in interactive mode,
> with
> no error or warning, wether I use -bail or not. I was expecting a hard
> error.
>

As usual, sqlite doesn't touch the DB file until it is asked to. Try
"sqlite3 FILENAME 'pragma schema_version'" on some random file and you'll
get "Error: file is encrypted or is not a database". But note that trying
the same on a non-existent file will succeed, and additionally create an
empty file.

Technically from sqlite's perspective a non-existent or empty file is a
perfectly well-formed database, just one which happens to contain no data.


> You'd think sqlite3[.exe] is the best suited to figure out if a file is a
> valie SQLite database or not,
>

It still is: sqlite3 FILENAME 'pragma integrity_check'

Parsing the header doesn't tell you whether the DB is valid, but if that's
all you want to do I suggest the ubiquitous file(1) command which reports
"SQLite 3.x database" for a [non-empty] sqlite db file.

-Rowan
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to