dr.jekyllandme has asked for the wisdom of the Perl Monks concerning the following question:
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}) or die $DBI::errstr; # doing something here with $dbh... $dbh->disconnect;
Is this right? Thank you for the help.#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What to do when DBI connect fails
by davido (Cardinal) on Oct 07, 2013 at 23:00 UTC | |
|
Re: What to do when DBI connect fails
by Marshall (Canon) on Oct 07, 2013 at 23:14 UTC | |
by YordanGeorgiev (Acolyte) on Jan 07, 2019 at 21:10 UTC |