Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

SQLite test database file

by greenFox (Vicar)
on Apr 19, 2006 at 07:46 UTC ( #544278=perlquestion: print w/replies, xml ) Need Help??
greenFox has asked for the wisdom of the Perl Monks concerning the following question:

I have a couple of little apps written with Perl/Tk and SQLite. Since I didn't want my apps opening each others databases I added a table (called global) with a column "application" and then I test for that having the correct value. Which worked well enough except if the table "global" didn't exist or the file selected for opening wasn't an SQLite database in which case I got error messages- not unexpected but I wanted my app to handle the situation more gracefully. I hacked around with it and came up with the below which behaves but seems a bit kludgy... is there a better way?

sub check_database_app { my $check_for = $_[0] || die "app name not passed!\n"; local $SIG{'__WARN__'} = sub { my $err = "eat the error $_[0]"; }; my $sth = $DBH->prepare("SELECT application FROM global") or die "Couldn't prepare statement: " . $DBH->errstr; $sth->execute() or do { $sth->errstr() =~ /no such table/i and return 0; die "Unknown error: " . $sth->errstr(); return 0; }; my $app = ($sth->fetchrow_array())[0]; $app eq $check_for ? return 1 : return 0; }

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

Replies are listed 'Best First'.
Re: SQLite test database file
by Corion (Pope) on Apr 19, 2006 at 08:02 UTC

    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.

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://544278]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-05-24 23:10 GMT
Find Nodes?
    Voting Booth?