2

我有这样的查询

set @valid_total:=0;
set @invalid_total:=0;
select week as weekno, measured_week,project_id as project, 
role_category_id as role_category,
valid_count,valid_tickets,
(@valid_total := @valid_total + valid_count) as valid_total, 
invalid_count,invalid_tickets, 
(@invalid_total := @invalid_total + invalid_count) as invalid_total
from metric_fault_bug_project 
where measured_week = yearweek(curdate())
and role_category_id = 1 and project_id = 11;

它在 heidi(MySQL 客户端)中执行得很好,但是当涉及到 perl 时,它给了我这个错误

DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near ':=0;
                                set :=0;
                                select week as weekno, measured_week,project_id
as project' at line 1 at D:\Mx\scripts\test.pl line 35.
Can't execute SQL statement: You have an error in your SQL syntax; check the man
ual that corresponds to your MySQL server version for the right syntax to use ne
ar ':=0;
                                set :=0;
                                select week as weekno, measured_week,project_id
as project' at line 1

问题似乎在set @valid_total := 0;一线。

我对 Perl 相当陌生。任何人都可以帮忙吗?

这是完整的 perl 代码

#!/usr/bin/perl

#use lib '/x01/home/kalpag/libs';


use DBI;
use strict;
use warnings;

my $sid = 'issues';
my $user = 'root';
my $passwd = 'kalpa';
my $connection = "DBI:mysql:database=$sid;host=localhost";


my $dbhh = DBI->connect( $connection, $user, $passwd) ||
            die "Database connection not made: $DBI::errstr";


my $sql_query = 'set @valid_total:=0;
         set @invalid_total:=0;
         select week as weekno, measured_week,project_id, 
                 role_category_id as role_category,
         valid_count,valid_tickets,
             (@valid_total := @valid_total + valid_count) as valid_total, 
                 invalid_count,invalid_tickets, 
         (@invalid_total := @invalid_total + invalid_count) as invalid_total
         from metric_fault_bug_project 
         where measured_week = yearweek(curdate())
         and role_category_id = 1 and project_id = 11';

 my $sth = $dbhh->prepare($sql_query) or die "Can't prepare SQL statement:     $DBI::errstr\n";

 $sth->execute() or die "Can't execute SQL statement: $DBI::errstr\n";

while ( my @memory = $sth->fetchrow() )
{
            print "@memory \n";
}
4

2 回答 2

1

您可能使用双引号字符串作为查询字符串,在这种情况下 perl 会查找变量@valid_total@invalid_total. 这意味着您没有使用

use strict;
use warnings;

因为否则你已经知道错误了。结果是 perl 将变量替换为空,这反映在您的错误中。

您需要做的是单引号字符串:

my $query = 'set @valid_total:=0;
set @invalid_total:=0;
select week as weekno, measured_week,project_id as project, 
role_category_id as role_category,
valid_count,valid_tickets,
(@valid_total := @valid_total + valid_count) as valid_total, 
invalid_count,invalid_tickets, 
(@invalid_total := @invalid_total + invalid_count) as invalid_total
from metric_fault_bug_project 
where measured_week = yearweek(curdate())
and role_category_id = 1 and project_id = 11';
于 2012-06-08T16:13:21.163 回答
0

请始终使用严格和警告。如果您没有变量@valid_total,则警告会警告您

Possible unintended interpolation of @valid_total in string

严格甚至会死。

在双引号字符串中,您必须转义 @ 符号:

"set \@valid_total:=0;"

如果您根本不需要插值,最好使用单引号。

编辑:在我看到 perl 代码之后:

而不是做

my $sql_query = 'set @valid_total:=0;
         set @invalid_total:=0;
...';
 my $sth = $dbhh->prepare($sql_query)

你应该做:

my $sql_query = 'set @valid_total:=0';
my $sth = $dbhh->prepare($sql_query);
$sth->execute;
$sth->finish;

$sql_query = 'set @invalid_total:=0'
$sth = $dbhh->prepare($sql_query);
$sth->execute;
$sth->finish;
...

您不能在一个查询中执行多个语句。

于 2012-06-08T16:15:31.210 回答