Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:15:17+00:00 2026-06-08T20:15:17+00:00

So I started to get familiar with Perl and I wrote my first Db

  • 0

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
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:15:19+00:00Added an answer on June 8, 2026 at 8:15 pm

    Its usually done like this

    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);
    my $retval = $result_handled->execute();
    if(!$retval){
        #open a log file and write errors 
        writelog();
        die "Error executing SQL SELECT - $dbh->errstr";
    }
    while(my ($division_id,$region_id,$count ) = $result_handled->fetchrow_array()){....
     }
    
    ---------------------------------
    
    sub writelog{
       my $path = "/path/to/logfile";
       my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
       $year += 1900; 
       $mon++;
       my $currdate = "$mon$mday$year";
    
       $logfile = $path . "/$currdate.log";
       open (OUT, ">>$logfile");
       print OUT "There was an error encountered while executing SQL- $dbh->errstr \n";
       close(OUT);
    }
    

    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, DBI will have AutoCommit set to 1 (enabled) by default, unless explicitly disabled. So your transactions would be auto committed per insert, in order to handle the ATOMICITY of the entire transaction, you can disable autocommit and use $dbh->commit and $dbh->rollback to handle when you want to commit, or may be use some custom commit point (for larger sets of data).

    Or the below can be used while connecting to the DB

    $dbh = DBI->connect( "dbi:Oracle:abcdef", "username", "password" , {
      PrintError => 0,   ### Don't report errors via warn(  )
      RaiseError => 1    ### Do report errors via die(  )
    } );
    

    this would automatically report all errors via die. The RaiseError is 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

    perl your_perl.pl >> out.log 2>> err.log
    

    out.log will contain regular logs and err.log will contain errors (specifically thrown by DBI prepare() or execute() methods too). In this case, you also need to make sure you use proper verbiage in print or die so that the logs look meaningful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get started on opengl programming from the videotutorialsrock.com site. It
I'm trying to get started with USSD . I'm familiar with other forms of
Coming from web development I have been trying to get familiar with compiled programming,
Im trying to get started with app development. Isnt this the corrent JDK if
How do I get started designing and implementing a script interface for my .NET
Im trying to get started with silverlight for wp7 and I understand its Silverlight
I've been trying to get started with scipy, but the package is giving me
I've started a little project for myself to get more familiar and comfortable with
Ive started to get my head in a bit of a mix regarding how
I just get started using BDD in Rails application, but I'm not sure what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.