On Thu, Sep 19, 2019 at 10:20 AM Rowan Worth <row...@dug.com> wrote:

> On Thu, 19 Sep 2019 at 16:03, Dominique Devienne <ddevie...@gmail.com>
> > On Wed, Sep 18, 2019 at 6:43 PM Clemens Ladisch <clem...@ladisch.de>
> > > Peng Yu wrote:
> > > > Is there a better way to just return an exit status of 0 for
> > > > a sqlite3 DB file and 1 otherwise?
>
> > >   dd bs=16 count=1 < some.db > sqlite3-signature
> > >   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.
>
> 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.
>

Thanks. Good tip. Need double not single quotes on Windows. And error code
of 26 if a bit weird,
but this works well otherwise:

D:\>sqlite3 TypedEntity.h 'pragma schema_version'
Error: unrecognized token: "'pragma"

D:\>sqlite3 TypedEntity.h "pragma schema_version"
Error: file is not a database

D:\>echo %ERRORLEVEL%
26

D:\>sqlite3 SOME.db "pragma schema_version"
58

D:\>echo %ERRORLEVEL%
0


> > 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.
>

I'm well aware of that. We were discussing an alternative to the *nix file
command,
and integrity_check goes way beyond (and is way slower) than checkinga file
type.
You might as well through FK checks with foreign_key_check if you are going
there :).

pragma quick_check would be more appropriate, if one wants to go the extra
mile, w/o slowing things down too much.

But again, thanks for this tip. Good one. --DD
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to