Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I have the database names in an array and go trough them in a loop, I want to try and connect and perform a query and disconnect.</p> </blockquote> <p>If you're looping, there's no reason to open multiple databases. In your loop, open a database, do your stuff, and close.</p> <blockquote> <p>How can I connect to multiple databases in perl without it bailing out if it can't connect to one?</p> </blockquote> <p>I'm assuming that this is in your loop. You can always use <code>eval</code> whenever you are doing any Perl command that might error out and stop your program from executing.</p> <p>You do something like this:</p> <pre><code>for my $database (@database_list) { my $dbh; eval { $dbh = DBI-&gt;connect($database, $user, $password); }; if (not $@) { yadda, yadda, yadda } } </code></pre> <p>The <code>eval</code> will catch any sort of ordinary deadly error. If <code>$@</code> has a value, the call failed, and <code>eval</code> returned an error description. If <code>$@</code> is empty, no error, and you can simply continue.</p> <p><em><strong>HOWEVER</em></strong>, BY default, DBI doesn't automatically die if it can't connect. Instead, it merely returns an undefined value. You should be able to use that to determine whether you've succeeded, or need to go to the next database:</p> <pre><code> for my $database (@database_list) { my $dbh = DBI-&gt;connect($database, $user, $password); if ($dbh) { yadda, yadda, yadda } } </code></pre> <p>If I remember correctly, there's an attribute called <a href="http://search.cpan.org/~timb/DBI/DBI.pm#RaiseError" rel="nofollow">RaiseError</a> that if set will cause your program to die in a failed DBI call. However, the default should be not set, so you shouldn't be having any issues.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload