-1

每当我执行下面的程序时,我都会Can't call method "execute" on an undefined value从以下行收到错误消息:

$sth->execute($agent_name,$service_id,$call_start_time,$call_end_time);

但在同一个程序中,我能够执行下面评论中指出的第一个 SQL 查询:

#!/usr/bin/perl -w
use strict;
use DBI;

my $DSN = q/dbi:ODBC:SQLSERVER/;
my $uid = q/ivr/;
my $pwd = q/ivr/;
my $DRIVER = "Freetds";
my $dbh = DBI->connect($DSN,$uid,$pwd) or die "Coudn't Connect SQL";
my $servernumber = 2;

my $service_name = "JM";
my $agent_name= 'Balaji';
my $call_start_time='2013-07-01 15:46:50.865';
my $call_end_time='2013-07-15 15:46:50.789';
my $call_rec_file_name;

my $rows_fund = $dbh->selectrow_array("select count(service_name) from cti_services  where service_name='$service_name'");
my $rows_agent = $dbh->selectrow_array("select count(agent_name) from cti_agents where agent_name='$agent_name'");

# This query successfully executes:
my $sql_fund = "select service_id from cti_services where service_name='$service_name'";
my $sth_fund = $dbh->prepare($sql_fund);
$sth_fund->execute() or die $DBI::errstr;
my $service_id = $sth_fund->fetchrow();
print $service_id,"\n";

if( $rows_fund == 1 && $rows_agent == 1 )
{
    my $sql="select top(10) service_name,agent_name,call_rec_file_name,call_start_time,call_end_time from cti_agents join  cti_call_master on (agent_name = call_agent_name) join cti_services on (call_service_id = service_id) where agent_name = ? and call_rec_file_name is not null and service_id=? and call_start_time between ? and ?";

    my $sth = $dbh->prepare($sql);

    # The problem is with this query. I'm getting the error "Can't call method "execute" on an undefined value".
    $sth->execute($agent_name,$service_id,$call_start_time,$call_end_time);

    print "Service Name","Agent Name","Call Start Time ","Call End Time","Sound File " ;
    while (my @data = $sth->fetchrow_array())
    {
        my ($service_name,$agent_name,$call_rec_file_name,$call_start_time,$call_end_time ) = @data;
        print "$service_name","$agent_name ","$call_start_time ","$call_end_time ","  $call_rec_file_name ";
    }
}
else
{
    print "<em>","There is no data found","</em>";
}

$dbh->disconnect;

什么可能导致错误消息?

4

3 回答 3

3

$sth未定义,因为您的调用$dbh->prepare因某种原因失败。

如果你用以下代码替换你的 DBI->connect() 调用,你会从prepare调用中得到错误,而不是当你尝试调用时它会静默失败并爆炸execute

my $dbh = DBI->connect($DSN,$uid,$pwd, { RaiseError => 1 });

您可以在此处阅读有关 RaiseError 和 DBI 调用可用的其他属性的更多信息:https ://metacpan.org/module/DBI#RaiseError

我必须承认我不能立即看到错误,我的直觉是 SQL 语法问题,但我不会说 SQLServer。

于 2013-07-27T10:32:47.150 回答
1

把里面的前两行改成if这个

my $sth = $dbh->prepare(<<__SQL__) or die $dbh->errstr;
SELECT TOP(10) service_name, agent_name, call_rec_file_name, call_start_time, call_end_time
FROM cti_agents
JOIN cti_call_master ON (agent_name = call_agent_name)
JOIN cti_services ON (call_service_id = service_id)
WHERE agent_name = ?
AND call_rec_file_name IS NOT NULL
AND service_id = ?
AND call_start_time BETWEEN ? AND ?
__SQL__

然后你会看到错误的原因。

请注意,前后不能有空格__SQL__

于 2013-07-27T13:40:42.707 回答
0

在 sql 中为日期时间加上引号,它可能在解析过程中看到空白?

于 2014-01-11T08:17:56.737 回答