Dump prepared sql query from DBI statement in PERL

前端 未结 3 2126
忘掉有多难
忘掉有多难 2020-12-15 12:22

im using DBI in Perl to connect to my PostgreSQL Database. Everything is working fine but in my debugging (printing results etc.) iam not able to see if the query prepared b

相关标签:
3条回答
  • 2020-12-15 12:37

    Use the DBI tracing facility. It works like this:

    use strict;
    use warnings;
    use DBI;
    my %opt = ( RaiseError => 1 );
    my $dbh = DBI->connect( 'dbi:mysql:test', 'fred', 'secret', \%opt );
    $dbh->trace(2); # level 2 shows statement with inserted parameters
    my $sql_i = 'insert into t1 (a, b) values ( ?, ? )';
    my $sth_i = $dbh->prepare( $sql_i );
    for ( qw/ eins zwei drei / ) {
        $sth_i->execute( $_, $_ );
    }
    $dbh->disconnect;
    
    0 讨论(0)
  • 2020-12-15 12:49

    DBI supports the following: There is the DBI->trace($tracefile_handle) method (traces all DBI interactions), or $dbh->trace($tracefile_handle) which would trace just the interactions on a specific handle. Output defaults to STDERR, but by supplying $tracefile_handle, you can explicitly send output to a different file (or just use shell redirection).

    DBD::pg also supports $h->trace('SQL'); This must be supported by your DBD driver to work, but fortunately DBD::Pg does support the feature.

    The documentation for DBI, at CPAN - DBI, and for DBD::Pg at CPAN - DBD::Pg really gives you all you need to know on tracing.

    0 讨论(0)
  • 2020-12-15 13:03

    Beyond the tracing others have mentioned you should look at https://metacpan.org/pod/DBI#Statement which gives you the SQL last executed and https://metacpan.org/pod/DBI#ParamValues and https://metacpan.org/pod/DBI#ParamTypes which tell you about your parameters.

    There is also DBIx::Log4perl which can log what you want without all the DBI tracing.

    0 讨论(0)
提交回复
热议问题