How can I print the SQL query executed after Perl's DBI fills in the placeholders?

前端 未结 8 795
梦毁少年i
梦毁少年i 2021-02-05 09:36

I\'m using Perl\'s DBI module. I prepare a statement using placeholders, then execute the query.

Is it possible to print out the final query that was executed without ma

8条回答
  •  无人共我
    2021-02-05 09:57

    If you don't want to create your own tracer module (as suggested by Sinan), you are better off just trying to print the argument hash before it is passed to $sth->execute(). This is especially true, since the "Trace" functionality is DBMS dependent and $sth->{Statement} only returns the SQL placeholder statement. Here's what I did.

    ...
    while (my $row = $csv->getline_hr($fh)) {
        my $cval = "";
        my $tquery = $query;
        foreach my $j (@cols) { 
                $cval = $row->{$j};
                $tquery =~ s/\?/\'$cval\'/;
        }
        print "$tquery\n\n";
        $rc = $sth->execute(@{$row}{@cols});
    }
    

    Where I have used Text::CSV... NOTE: This is not exact, due to DBMS implementation dependent handling of {'}s.

提交回复
热议问题