简体   繁体   English

Perl,DBI和MySQL分隔符

[英]Perl, DBI and the MySQL delimiter

I have a need to be able to issue "create trigger" over DBI. 我需要能够在DBI上发出“创建触发器”。 I can't seem to get the delimiter command working. 我似乎无法让delimiter命令工作。 Can anyone find a way to make this work? 任何人都可以找到一种方法来使这项工作?

Code: 码:

use strict;
use DBI;
my $dbargs = {mysql_auto_reconnect => 1,
              AutoCommit           => 0,
              RaiseError           => 1,
              ShowErrorStatement   => 1}; 

my $dsn = "DBI:mysql:database=xdisp;host=cycldev06";
my $dbh = DBI->connect( $dsn, 'sqluser', '', $dbargs);

my $sql = qq{ 
    DELIMITER // 
        CREATE TRIGGER `hardware_last_status` BEFORE UPDATE
            ON `hardware` FOR EACH ROW BEGIN
                                IF NEW.status != OLD.status AND NEW.last_status = OLD.last_status THEN
                                    SET NEW.last_status = OLD.status;
                                END IF;
                            END
            //
};

$dbh->do($sql);

Results: 结果:

DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER // 
        CREATE TRIGGER `hardware_last_status` BEFORE UPDATE
      ' at line 1 at test.pl line 24.

and that SQL works fine at the MySQL command line. 并且SQL在MySQL命令行中运行良好。

The delimiter command is used by the client program to determine the limits of the SQL statement. 客户端程序使用delimiter命令来确定SQL语句的限制。 It is (almost certainly) not seen by the server itself. 它(几乎可以肯定)没有被服务器本身看到。 Therefore, in Perl + DBI, you should simply omit the delimiters. 因此,在Perl + DBI中,您应该省略分隔符。 Thus, the command you should execute is: 因此,您应该执行的命令是:

my $sql = qq{ 
    CREATE TRIGGER `hardware_last_status` BEFORE UPDATE
        ON `hardware` FOR EACH ROW BEGIN
                            IF NEW.status != OLD.status AND NEW.last_status = OLD.last_status THEN
                                SET NEW.last_status = OLD.status;
                            END IF;
                        END
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM