Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; }

In reply to Using DBI to transfer data from one database to another by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found