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
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;
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.
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.