从PERL中的DBI语句转储准备sql查询 [英] Dump prepared sql query from DBI statement in PERL

查看:172
本文介绍了从PERL中的DBI语句转储准备sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Perl中的DBI连接到我的PostgreSQL数据库。
一切都工作正常,但在我的调试(打印结果等)中,我无法看到Perls DBI模块准备的查询是否正确。



我有这样的东西:

  $ sth-> prepare(qq {SELECT * FROM company WHERE companyname LIKE?AND city = ?}); 
$ sth-> execute($ name。'%',$ city);

Iam无法查看调用执行后sql查询的外观,因为执行是最新的步骤参数绑定到查询。



我想要有一些类似 $ sth-> getLastExecutedQuery()或者看看查询的样子。



在这种情况下,函数 getLastExecutedQuery()将返回: / p>

  SELECT * FROM company WHERE companyname LIKE'公司名称%'AND city ='City name'; 

有没有办法得到这个?它仅用于调试目的。

解决方案

DBI支持以下内容:
有$ code> DBI - > trace($ tracefile_handle)方法(跟踪所有DBI交互),或 $ dbh-> trace($ tracefile_handle)只是在特定句柄上的交互。输出默认为STDERR,但是通过提供 $ tracefile_handle ,您可以将输出显式发送到其他文件(或仅使用shell重定向)。



DBD :: pg还支持 $ h-> trace('SQL'); 这必须由您的DBD驱动程序支持,但幸运的是DBD :: Pg确实支持该功能。



DBI的文档位于 CPAN - DBI
和DBD :: Pg在 CPAN - DBD :: Pg 真正给你所有你需要知道的追踪。


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 by perls DBI module is really correct.

I have something like this:

$sth->prepare( qq{SELECT * FROM company WHERE companyname LIKE ? AND city = ?});
$sth->execute( $name.'%', $city);

Iam not able to see how the sql query looks after calling execute, as execute is the latest step where parameters are binded to the query.

I would like to have something like $sth->getLastExecutedQuery() or something to see how the query looked like.

In this case the function getLastExecutedQuery() would return:

SELECT * FROM company WHERE companyname LIKE 'Company Name%' AND city = 'City name';

Is there any way to get this? Its only for debugging purposes.

解决方案

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.

这篇关于从PERL中的DBI语句转储准备sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆