I'm writing an insert_row function using DBI. I get the following error:
DBD::DB2::st execute failed:
IBMCLI Driver...blah.......The statement string of the PREPARE or EXECUTE IMMEDIATE statement is blank or empty. SQLSTATE=42617. Can't call method execute on an undefined value.
I think it is a problem with my binding parameters. Here is the code:
sub insert_row{
if( $#_ < 2 ){
print 'USAGE: $rv = insert_row($DB_TYPE, $DB_HANDLE, $TABLE, $COL1
+, $COL2, $VAL2, $VAL2)';
return 0;
}
my $db_type = shift;
my $dbh = shift;
my $table = shift;
if( ($#_+1) % 2 != 0 ){
print "COLUMN/VALUE mismatch @ insert_row($db_type,$dbh,$table...)
+";
return 0;
}
my $num_cols = ($#_+1)/2;
my $val_index = ($#_+1)/2;
my $col_index = 0;
my %columns = ();
my $x = 0;
for($x = 0, $x < $num_cols, $x++){
$columns{$_[$col_index]} = $_[$val_index];
$col_index++;
$val_index++;
}
my $sql = "INSERT INTO " . $table . " (";
while ( my ($key, $value) = each(%columns) ) {
$sql .= "$key,";
}
chop $sql;
$sql .=") VALUES (";
for( $x = 0; $x < $num_cols; $x ++ ){
$sql .= '?,';
}
chop $sql;
$sql .= ')';
my $sth = $dbh->prepare($sql);
my @bind_values = ();
while ( my ($key, $value) = each(%columns) ) {
push @bind_values, $value;
}
return $sth->execute(@bind_values);
}
In reply to Insert Row
by Losing
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.