Yeah, with the Look Before You Leap style there's that chance, but since the 
default behavior is to create the file if it's not there, then connecting to 
some file name is never gonna fail (unless the absolute path directory doesn't 
exist)

With the CLI for example I know I've created tons of blank files by hitting the 
wrong character, or having shift held down too long when I'm attaching things.

attach database 'January.sqlite' as jan;
attach database 'FEbruary.sqlite' as feb;

Oops, just created new file with a capital E, and won't notice it until I run a 
query and see "Error: no such table: feb.theTableYouWereLookingFor"



-----Original Message-----
From: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org] On 
Behalf Of Random Coder
Sent: Thursday, April 06, 2017 6:07 PM
To: SQLite mailing list
Subject: Re: [sqlite] Testing sqlite db to see if exists & ready

On Thu, Apr 6, 2017 at 2:07 PM, David Raymond <david.raym...@tomtom.com> wrote:
> Before opening the connection you could do something along the lines of
>
> if not os.path.isfile(fi) or not os.access(fi, os.W_OK):
>     print "File isn't there or isn't writable"
>     return 1
> with open(fi, "r") as f:
>     if f.read(16) != "SQLite format 3\x00":
>         print "Magic header isn't correct"
>         return 1

If you do something like this, be prepared for another process to have
created the database for you after your check suggested it doesn't
exist.

Might not matter in your exact use case, but if there's a chance for
two instances of your script to be running at once, you'll need to
handle this race condition.
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to