in reply to variable type

That shouldn't be an issue if you're using placeholders. DBI knows if it's suppose to be an number or a string based on information it obtains from the database.

sub escape_name { my $t = @_ ? $_[0] : $_; $t =~ s/`/``/g; return "`$t`"; } my $table = 'sometable'; my @cols = qw( id name ); my @vals = qw( 3 somename ); my $stmt = "INSERT INTO " . escape_name($table) . " (" . join(', ', map escape_name, @cols) . ") VALUES (" . join(', ', (?) x @cols) . ")"; my $sth = $dbh->prepare($stmt); $sth->execute(@vals);

I'm using MySQL's escape mechanism for table names and column names. I don't know if DB2 uses the same mechanism.

Replies are listed 'Best First'.
Re^2: variable type
by ikegami (Patriarch) on Dec 10, 2006 at 01:39 UTC

    $dbh->quote_identifier is the portable version of escape_name

    my $table = 'sometable'; my @cols = qw( id name ); my @vals = qw( 3 somename ); my $stmt = "INSERT INTO " . $dbh->quote_identifier($table) . " (" . join(', ', map { $dbh->quote_identifier($_) } @cols) . ") VALUES (" . join(', ', (?) x @cols) . ")"; my $sth = $dbh->prepare($stmt); $sth->execute(@vals);