Perl's
DBI interface is excellent; however, it is intended for SQL not PL/SQL. If you do need procedures run, the traditional method is to use stored procedures invoked via standard queries (SELECT, INSERT...). If I was going to implement this, I would do the logic and looping in Perl, and the database interaction via DBI. Something like (untested):
use strict;
use warnings;
use DBI;
my $db = DBI->connect( $dbid,
$user,
$password,
{
PrintError => 0,
RaiseError => 1,
AutoCommit => 0,
},
);
my $query = $db->prepare(<<EOSQL);
INSERT INTO temp (field1, field2, field3)
VALUES(?, ?, ?)
EOSQL
my $x = 100;
for my $i (1 .. 10) {
$query->execute($i, $x, $i % 2 ? 'i is odd' : 'i is even');
$x += 100;
}
$db->commit;
Note that I've used a single query with
placeholders rather than interpolation of values. This is both more efficient and protects you from SQL-injection and escaping issues. I've also taken advantage of the fact that 1 is true in Perl, thus bypassing an equality check. Finally, I explicitly included my target fields in the INSERT, since this makes you resilient to schema changes and makes maintenance more obvious.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.