在perl中执行过程查询

时间:2012-06-08 16:01:26

标签: mysql perl

我有这样的查询

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";
}

2 个答案:

答案 0 :(得分: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';

答案 1 :(得分:0)

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

Possible unintended interpolation of @valid_total in string

严格甚至会死。

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

"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;
...

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

相关问题