gpt4 book ai didi

mysql - DBD::mysql::st 执行失败:MySQL 服务器已离开

转载 作者:可可西里 更新时间:2023-11-01 07:46:54 26 4
gpt4 key购买 nike

我想从一个fifo管道中插入数据到一个mysql表中

我的脚本如下:

#!/usr/bin/perl
#Script to read data out of a named pipe and write to MySQL database.

$| = 1;

use strict;
use DBI();

my $filename;
my $inputline;
my $linenumber;
my @arr;
$filename = "./SEC_fifo";
open(FIFO, "+< $filename") or die "FIFO error on $filename $!";
my $dbh = DBI->connect("DBI:mysql:database=ecdb;host=localhost",
"user", "[pwd]",
{'RaiseError' => 1});
while (<FIFO>)
{
$inputline = $_;
@arr = split(/,/,$inputline);

# Quit read loop when requested.
last if($inputline =~ /quit/i);

chop $inputline;
$linenumber++;
print "Got: [$inputline], ";
my $sql="";
my $sth="";
my @row;
print "output.\n".$arr[0],$arr[1],$arr[2],$arr[3],$arr[4],$arr[5],$arr[6],$arr[7],$arr[8],$arr[9],$arr[10],$arr[11],$arr[12],$arr[13]."\n";
# perl trim function - remove leading and trailing whitespace
my $str = $arr[6] ;
$str =~ s/^\s+//;
$str =~ s/\s+$//;

if($str ne 'Normal')
{
print "arr[6]=".$arr[6]."\n";
$sql = "select Hid from Devices where hostname = '$arr[2]'";
print $sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql
\n";
@row=$sth->fetchrow_array;
my $hid1=@row[0];
$sql = "select Hid from Devices where hostname = '$arr[3]'";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $hid2=@row[0];
$sql = "select Eid from Event where Eventname = '$arr[8]' and severity = '$arr[9]' and Trapoid='$arr[10]'";
print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $eid1=@row[0];
#print "eid1=".@row[0]."\n";
$sql = "select Eid from Event where Eventname = '$arr[11]' and severity = '$arr[12]' and Trapoid='$arr[13]'";
#print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $eid2=@row[0];
print "eid1=".$eid1."\n";
print "eid2=".@row[0]."\n";
$sql = "insert into secresult (Hid_1,Hid_2,interface_1,interface_2,ifindex_1,ifindex_2,Eid_1,Eid_2,start_time_1,start_time_2,effect_range,description) values ($hid1,$hid2,'$arr[4]','$arr[5]',$arr[6],$arr[7],$eid1,$eid2,'$arr[0]','$arr[1]','$arr[14]','$arr[15]')";
print $sql."\n";
$dbh->do($sql);
print "inserted it.\n";

}
else
{
$sql = "select Hid from Devices where hostname = '$arr[1]'";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $hid=@row[0];

$sql = "select Eid from Event where trapoid = '$arr[0]' and severity != 'Normal'";
#print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $eid=@row[0];

$sql = "select id from (SELECT * FROM secresult WHERE end_time_1 ='' and Hid_1=$hid and interface_1 ='$arr[4]' and ifindex_1=$arr[5] ORDER BY start_time_1 DESC LIMIT 1) result where result.eid_1 = $eid";
print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
my $id=@row[0];
if($id !="")
{
$sql = "update secresult set end_time_1 = '$arr[2]' where id = '$id'";
print $sql."\n";
$dbh->do($sql);
print "updated it.\n";
}
else
{
$sql = "select id from (SELECT * FROM secresult WHERE end_time_2 ='' and Hid_1=$hid and interface_2 ='$arr[4]' and ifindex_2=$arr[5] ORDER BY start_time_2 DESC LIMIT 1) result where result.eid_2 = $eid";
print "sql=".$sql."\n";
$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";
@row=$sth->fetchrow_array;
$id=@row[0];
$sql = "update secresult set end_time_2 = '$arr[2]' where id = '$id'";
print $sql."\n";
$dbh->do($sql);
print "updated it.\n";

}

}

}

my $sth = $dbh->prepare("SELECT * FROM secresult"); $sth->execute(); while (my $ref = $sth->fetchrow_hashref()) {
print "Found a row: id = $ref->{'id'}, line = $ref->{'textline'}\n"; }

$sth->finish();

$dbh->disconnect();

exit;

我可以通过脚本插入到数据库中,但是当我执行脚本进行监听时,几分钟后从 fifo 获取数据,脚本将显示错误消息“DBD::mysql::st execute failed: MySQL服务器已经离开 ./Db_code.pl 第 66 行,第 5 行”

第 66 行是“$sth->execute || die”Could not execute SQL statement ... maybe invalid?\n\n $sql\n";"

最佳答案

在任何数据库操作和重新连接(或克隆)之前确保 dbh 仍然连接(ping)

while (<FIFO>)
{
if ( ! $dbh->ping ) {
$dbh = $dbh->clone() or die "cannot connect to db";
}

...

} #end FIFO

另见 http://www.perlmonks.org/?node_id=497849

关于mysql - DBD::mysql::st 执行失败:MySQL 服务器已离开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23866132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com