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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.