sub insert { my( $table, $data, $key ) = @_; my $t = $key ? "$table-$key" : $table; if( !defined $dbo->{$t} ) { # first time through, set some things up my $db = DBI->connect(DB_IN_SRV, DB_IN_DB, DB_IN_PW, {AutoCommit => AUTOCOMMIT, PrintError => 0}) or die "Couldn't connect to database @{[DB_IN_SRV]} " . DBI->errstr; my $s = [sort keys %$data]; my $sql = "insert into $table(" . join(',' => @$s) . ')values(' . join( ',' => ('?') x @$s ) . ')'; my $ss = $db->prepare($sql) or die "[$table/$t] prepare $sql\n" . $db->errstr . "\n"; @{$dbo->{$t}}{qw/DB STH SQL SORT COUNT ERR/ } = ($db, $sth, $sql, $s, 0, 0); } ++$dbo->{$t}{COUNT}; if( !$dbo->{$t}{STH}->execute( @$data{ @{$dbo->{$t}{SORT}} } ) ) { warn $dbo->{$t}{COUNT}, ': ', $dbo->{$t}{SQL}, "\n", join( "\n", @$data{ @{$dbo->{$t}{SORT}} } ), "\n", $dbo->{$t}{DB}->errstr, "\n\n"; ++$dbo->{$t}{ERR}; # give up quickly if things are going worng if( $dbo->{$t}{COUNT} > 10 and $dbo->{$t}{ERR} > 0.6 * $dbo->{$t}{COUNT} ) { die "Giving up.\n"; } } AUTOCOMMIT or $dbo->{$t}{COUNT} % COMMIT_BATCH or $dbo->{$t}{DB}->commit; }