On 06 Apr 2017 at 13:19, Simon Slavin <slav...@bigfraud.org> wrote: 

> On 6 Apr 2017, at 12:11pm, Tim Streater <t...@clothears.org.uk> wrote:
>
>> When my app starts, I check that the file in question actually *is* a
>> database by doing some simple steps like open, selects from important tables,
>> and a read/write to a globals table in the database that contains, for
>> instance, the version number.
>
> Don’t do this.  Because if the file isn’t there, or if the file is there
> but has zero length, SQLite will turn it into a SQLite file and then return
> results which don’t let you tell whether the file was already there or just
> created.  And you probably don't want this.

That would appear not to be the case. Under OS X 10.9.5, I touched a 
non-existent file and then using sqlite3.app did:

.schema                        <--- gave nothing
select version from globals;   <--- gave "Error: no such table"

My file stayed at zero bytes long.

I then added a bit of text to the file and repeating the exercise:

.schema                        <--- gave nothing
select version from globals;   <--- gave "Error: file is encrypted or is not a 
database"

The file was not modified by these actions.

> Instead use PHP functions to check that the file exists using PHP function
> "file_exists()" and then using fread() to read the first 16 bytes from it. 
> Those 16 bytes should be "SQLite format 3" followed by a 0x00 byte for a
> string terminator.

Actually I am scanning the directory and examining all the files there. The 
sequence of tests is: open, read from the two expected tables, write back to 
one of them. This confirms that the file is a database, it's one I'm looking 
for, it has the expected tables, and is writeable too. I keep a database of 
such files which is then re-populated with successful entries.

This sequence was subject to careful testing when I first wrote it. Each step 
is under try/catch and I only proceed to the next step if previous ones 
succeeded.

> I’m not sure what you mean by "unconfigured" so I’ll let other people
> write about that, or you can post to clarify.

I'm not sure either, better ask the OP.

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

Reply via email to