So I started to get familiar with Perl and I wrote my first Db script.
Now I am trying to select data from atable which is huge and trying to insert into a summary table based on some criteria.
Now there are chances , that select query may fail or the insert query may fail due to timeout or other database issues that is beyond my control.
Eventually my script is going to be cron script.
Can I log just the errors that i encounter for the connection,inserts and selects into a file generated in the script?
$logfile = $path.'logs/$currdate.log';
here is my code:
my $SQL_handled="SELECT division_id,region_id, NVL(COUNT(*),0) FROM super_tab GROUP BY division_id,region_id;";
my $result_handled = $dbh->prepare($SQL_handled);
$result_handled->execute();
while (my ($division_id,$region_id,$count ) = $result_handled->fetchrow_array()){
my $InsertHandled="INSERT INTO summary_tab (date_hour, division_id, region_id,volume) VALUES ('$current',$division_id,$region_id,$market_id,'$service_type','$handled',$count);";
my $result_insert_handled = $dbh->prepare($InsertHandled);
$result_insert_handled->execute();
}
something like
if(DBI-query failed ) {
// log the error onto the above logpath
}
Its usually done like this
You can also use
$dbh->err;which returns the native Oracle error code to trap the error and exit accordingly.The above, basic exception handling can be performed for every
execute()method call in your script. Remember,DBIwill haveAutoCommitset to1(enabled) by default, unless explicitly disabled. So your transactions would be auto committed per insert, in order to handle theATOMICITYof the entire transaction, you can disable autocommit and use$dbh->commitand$dbh->rollbackto handle when you want to commit, or may be use some customcommit point(for larger sets of data).Or the below can be used while connecting to the DB
this would automatically report all errors via
die. TheRaiseErroris usually turned off by default.Also if I understand you correctly then, by cron you mean you would be calling it from a shell cron job. In that case, call to your perl script from the cron itself can be redirected to log files something like below
out.logwill contain regular logs anderr.logwill contain errors (specifically thrown by DBIprepare() or execute()methods too). In this case, you also need to make sure you use proper verbiage inprintordieso that the logs look meaningful.