in reply to correct way to do multi-insert/update transaction

I might do things a little differently, but this will generally do what you need from an algorithmic stand point. There are two syntax errors in your code:
  1. In a normal for loop, your list has to be delimited by parentheses. See Foreach Loops in perlsyn.
  2. You use both $dbh and $db for your database handle. Without strict, this would have given you a confusing error. See Use strict warnings and diagnostics or die or The strictures, according to Seuss.

You might also benefit from a read through Basic debugging checklist. In the end, your code might look something closer to

#!/usr/bin/perl use DBI; use strict; use warnings; use diagnostics; # create database connection my $dbh = DBI->connect("DBI:mysql:database=somedb;host=localhost", "user", "pass", { 'RaiseError' => 1, 'AutoCommit' => 0, }, ); # trap errors using eval{} my $insert_sth = $dbh->prepare("insert into table (field) values(?)"); eval { #go around some loop to get a value for (1...100000) { $insert_sth->execute($_); } # commit changes $dbh->commit(); }; # any errors # rollback if ($@) { print "Transaction aborted: $@"; $dbh->rollback(); } # close connection $dbh->disconnect();

Replies are listed 'Best First'.
Re^2: correct way to do multi-insert/update transaction
by Anonymous Monk on Apr 28, 2011 at 13:40 UTC
    thanks - is there a way to award credit or points for an answer?
      You're welcome.

      There is, but you have to have an account and have hung around the monastery long enough to earn the right to vote. See Voting/Experience System.