http://qs321.pair.com?node_id=544282


in reply to SQLite test database file

While I have no help or comment on how to deal with a corrupt SQLite file, and others are more competent to comment on how to handle the case of a missing table (I guess eval and RaiseError are a good solution), I'm not sure if your practice of expecting/selecting exactly one row from the table is sound. I would allow for more than one row in the global table or convert the existence of more than one row into an error:

If you only ever want one application to access one database file, I'd use the following SQL to check that there is no other application that is allowed to access that database:

SELECT * WHERE application <> ? -- and SELECT * WHERE application = ?

and then check that the first statement returns 0 rows and the second statement returns exactly one row.

If you want more than one application to access a table, you can then leave out the first query.

Update: A quick googling shows me this page of DBI recipes, likely by gmax, which has a routine to check for existing tables. If a file is completely missing, likely the database doesn't exist either. I'm not sure how to ask SQLite if a file is a valid SQLite database.

Second update: Changed the SQL; replaced the count(*) by * so it reflects what my text says.

Replies are listed 'Best First'.
Re^2: SQLite test database file
by greenFox (Vicar) on Apr 19, 2006 at 08:10 UTC

    Good points, thanks- I am an SQL novice really :) I did try wrapping the $sth->execute() call in an eval but it didn't capture the error...

    Update: DBI recipes is indeed by gmax

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho