use strict; use warnings; use DBI; my $searchstring = "DROP TABLE mydata;"; $dbh = DBI->connect("dbi:Pg:dbname=$dbname", 'myuser', 'verysecret', {AutoCommit => 0}) or die("Can't connect to database"); my $sth=$dbh->prepare("SELECT * FROM mydata WHERE mycolumn = ?") or die($dbh->errstr); if(!$sth->execute($searchstring)) { $dbh->rollback; exit 500; # or whatever you want to do in case of statement level error } while((my $line = $sth->fetchrow_hashref)) { # do something with the data } $sth->finish; $dbh->rollback; # Since we didn't change anything, finish the transaction with a rollback instead of commit; just in case