in reply to beginner - looping to insert a set of database records

Your pseudocode above suffers from a problem: in creating your database handle, you set RaiseError to true. This means that your script will die on any database error, which is exactly what you don't want. At a bare minimum, you would likely want something closer to:

my $dbh = DBI->connect("DBI:mysql:database=$sql_database;host=$sql_hos +t;port=$sql_port", $username, $password, { RaiseError => 0, } ) or die "DB connect failed: $DBI::errstr"; for my $record (@records) { unless ($dbh->do($insertstr, undef, $slice->seq_region_name(), $vf->start $vf->variation_name(), $vf->allele_string() )) { print INSERTLOG "failed to insert", , "\n"; } #end unless }#end loop

However, if you are doing multiple inserts, I would suggest preparing a single statement outside the loop and then binding parameters and executing inside the loop. You'll also likely want to use $DBI::errstr to output more useful diagnostics. This is all discussed in DBI. See RaiseError, Placeholders_and_Bind_Values, $DBI::errstr, and PrintError.

Replies are listed 'Best First'.
Re^2: beginner - looping to insert a set of database records
by Anonymous Monk on Nov 12, 2010 at 23:12 UTC
    thanks