Another way is to fix the errors with the error handler, do whatever you need (send it to the log file, print, die or continue the script).
This eliminates the need for " or die() " after each method. The documentation for the HandleError method can be found here .
To get started, take this simple example:
#!/usr/bin/perl use strict; use warnings; use DBI; my $DB_name = 'database'; my $DB_user = 'root'; my $DB_pwd = ''; my $dsn = 'dbi:mysql:avm:localhost:3306'; my ($sth, $id, $name); my $dbh = DBI->connect($dsn,$DB_user,$DB_pwd, { PrintError => 0, ShowErrorStatement => 1, HandleError => \&dbi_error_handler,} ); $sth = $dbh->prepare("SELECT * FROM tblmanufacturer"); $sth->execute(); while ( ($id,$name) = $sth->fetchrow_array() ) { print "$id\t\t $name \n"; } $sth->finish(); $dbh->disconnect(); sub dbi_error_handler { my( $message, $handle, $first_value ) = @_;
PS There is no reason to encapsulate yout string variables in quotation marks here: ($dsn,"$DB_user","$DB_pwd"); do not do this, for more information on this, read this .
Radu maris
source share