【问题标题】:perl called with 2 bind variables when 0 are neededperl 在需要 0 时使用 2 个绑定变量调用
【发布时间】:2012-08-22 22:45:51
【问题描述】:

我有这个代码:

    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 子句。

【问题讨论】:

  • 什么是 $db?这不是典型的 DBI->do call...
  • do 方法的第二个参数中,您使用q{} 引号运算符。变量没有在里面插入,所以你传递了文字字符串$where。这是故意的吗?使用qq{} 运算符将$where 的内容放入您的查询中。
  • @BrianPhillips,我的 $db = DBIx::DataStore->new('www');
  • @amon - 这应该是一个答案,而不是评论! :-)

标签: sql perl dbi


【解决方案1】:

我认为您已将参数的顺序转换为do()

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

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

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

【讨论】:

  • 这是一个很好的建议,但它并没有解决 OP 遇到的实际问题。 $db 变量不是 DBI 句柄,SQL 需要双引号,以便插入 $where 子句。
  • 好的。我以为它是一个 DBI 句柄。
猜你喜欢
  • 2020-06-14
  • 2022-01-18
  • 2016-10-30
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多