tokuhirom's Blog

How do you trace your query with DBI

DBIx::QueryLog とかつかわなくても Callbacks を駆使すれば結構いけるよ!っていう話になった。いいね。

use strict;
use warnings;
use DBI;

my $dbh = DBI->connect('dbi:SQLite:','','', {
    RaiseError => 1,
    Callbacks  => {
        ChildCallbacks => {
            execute => sub {
                my ($obj, @binds) = @_;
                my $stmt = $obj->{Database}->{Statement};
                $stmt =~ s/\?/'$_'/ for @binds;
                print STDERR $stmt, "\n";
                return;
            },
        },
    },
});
$dbh->do(q{create table job (func, time)});

my $sth = $dbh->prepare('select * from job where func = ? AND time=?');
$sth->execute('abra', 'catabra');