On 06 Apr 2017 at 11:28, Clemens Ladisch <clem...@ladisch.de> wrote: 

> dave boland wrote:
>> Being a little paranoid, I like to insure that the db file exists
>
> SQLite automatically creates an empty DB if you try to open
> a nonexistent file, so you do not actually need to do anything.
>
>> and what state it is in (unconfigured, so needs to be made
>> ready; or ready to accept data (or be read)).  How do I do that?
>
> Store a DB version number somewhere.  (You can do it like Android and use
> PRAGMA user_version, or use an entry in some table.)  If the version
> number is not high enough, you have to create or update the database.
> Do everything in a transaction to prevent a partially-created/updated
> database:

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. I'm using PHP for this and it is a good use for try/catch at 
each step. If the step succeeds, I pass to the next. Since the database (one of 
many, in fact) belongs to the user, I can't hide it away somewhere. It also 
allows the user to add possibly older versions of the database file there, or 
completely other files such as READMEs. Files such as the latter will fail at 
an early step (usually with: file is not an SQLITE database) but I don't care 
why; they either pass or fail, and if they fail, the app then ignores them.

Older versions of the db can be spotted by the version number in the globals 
table (as Clemens say above); they can then be silently upgraded to the current 
version. This will happen if I have, for instance, added a column or two to 
support some new feature.

I don't user PRAGMA user_version because I keep reading that the continued 
existence of any particular PRAGMA is completely un-guaranteed.


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