I have a Perl script that connects to a sqlite3 databaase and does a query.
Example:
#!/usr/software/bin/perl5.8.8
use strict;
use warnings;
my $db = 'somedb';
my $dbh = DBI->connect("dbi:SQLite:dbname=$db", "", "", {RaiseErro
+r => 1}) or die $DBI::errstr;
# doing something here with $dbh...
$dbh->disconnect;
The above code works, but I would like to add a condition when the DBI->connect fails. Currently the script dies, but instead I would like to have an if statement that does something alternative if the db I am connecting to is not available. Then exits.
I was thinking of this:
#!/usr/software/bin/perl5.8.8
use strict;
use warnings;
my $db = 'somedb';
my $dbh = DBI->connect("dbi:SQLite:dbname=$db", "", "", {RaiseErro
+r => 1});
# Is this the right way to check if DBI connected to the database?
unless ($dbh) {
print "Unable to connect to $db.\n";
# doing something else here...
}
# doing something here with $dbh...
$dbh->disconnect;
Is this right?
Thank you for the help.