创建事件以删除会话时,SQL语法错误

时间:2019-01-18 17:24:17

标签: mysql session session-cookies

我对以下问题的答案中提供的SQL代码进行了一些修改:How to delete a MySQL record after a certain time 但是,每次运行查询时,都会出现“您的SQL语法有错误”错误。

    my $host ="$hash{Linux_Server_IP}";
    my $Uname = "$hash{Linux_Server_Username}";
    my $password ="$hash{Linux_Server_Password}";
    my $ssh2= Net::SSH2->new();

    $ssh2->connect($host)or die "Could not connect : $@ \n ";
    $ssh2->auth_password($Uname,$password) or die "Could not login : Reason : $@\n";
    $ssh2->blocking();
    our $chnl = $ssh2->channel();
    $chnl->shell();

print $chnl "ls | grep $_[1] \n";
my @output;
sleep(1);
while (<$chnl>){push @output,$_};
die "could not find the file $_[1] in $Source to $Destination \n" unless(@output);
print $chnl "cp $Source $Destination \n";

代码应创建一个事件,以在7天后从会话表中删除条目,但是却给了我这个错误。语法实际上有问题吗?

1 个答案:

答案 0 :(得分:1)

您的CREATE EVENT命令和DELETE命令正在使用;作为分隔符。因此,您的CREATE EVENT命令在DELETE命令之后(在END之前)结束。您需要在开始时设置DELIMITER才能在CREATE EVENT命令上使用另一个。

-- set the DELIMITER to "|"
DELIMITER |

CREATE EVENT delete_session
    ON schedule AT current_timestamp + INTERVAL 1 DAY
    ON COMPLETION PRESERVE
DO BEGIN
    DELETE FROM session WHERE `date` < DATE_SUB(NOW(), INTERVAL 7 DAY);
END |

-- set the DELIMITER back to the default.
DELIMITER ;

...,您还需要更改一件事:

  • 删除S上的尾随DAYS