当需要0时,perl使用2个绑定变量调用

时间:2012-08-22 14:34:07

标签: sql perl dbi

我有这段代码:

    my (@matches, @vars);  

    if ($date_to) {
            push(@matches, q{ m.mentioned_at <= ? });
            push(@vars, $date_to);
        }

        if ($person) {
            push(@matches, q{ t.name like ? });
            push(@vars, $person);
        }
    if ($show) {
            push(@matches, q{ t.name like ? });
            push(@vars, $show);
        }  
    if (@vars){
        my $where = join (' and ', @matches);
        $where = qq{where $where} if @matches  
        $res = $db->do({ page => $.p, per_page => $.s }, q{
                      select m.id, m.body, m.link,
                      count(distinct(m.id)) as num_items
                      from mentions m
                      $where 
                      group by m.id, m.body, m.link,
                      order by m.created_at desc
               }, @vars );
     }   

     print STDERR Dumper ($where);    
     $VAR1 = 'where  m.mentioned_at <= ?  and  t.name like ? ';`  
     print STDERR Dumper (@vars);   
     $VAR1 = '2012/06/01';  
     $VAR2 = 'Adby Phil';`  

为什么我有这个错误:

called with 2 bind variables when 0 are needed  
Can't use an undefined value as an ARRAY reference 

好像它没有看到$ where子句。

1 个答案:

答案 0 :(得分:3)

我认为您的参数顺序为do()已切换:

$rv  = $dbh->do($statement, \%attr, @bind_values);

首先看起来你有\%attr,然后是$ statement。

另外,请注意do()不返回语句句柄,因此无法获取数据。您可能需要更传统的准备和执行序列。