我尝试使用perl中的更新查询来更新数据库中的表。正在收到以下错误
SQL Error: couldnt update Server message number=20000 severity=0 state=0 line=0 server=AWSOSA1 text=ERROR=Parse failure on line 1 of statement 'UPDATE alerts.status SET AlertKey='Port 101 Circuit Up down' where Identifier='Link 101 Circuit Up Down Circuit Status Private Cloud uplinks CP0027829 DOWN 101 PC_SOCKET_PROBE', at or near '''
我试图打印查询并在db中运行它,并且在那里工作。不确定为什么通过perl脚本运行时会出现解析错误:-(
有人可以帮忙吗?
以下是我尝试通过perl运行的查询:
UPDATE alerts.status SET AlertKey='Port 101 Circuit Up down' where Identifier='Link 101 Circuit Up Down Circuit Status Private Cloud uplinks CP0027829 DOWN 101 PC_SOCKET_PROBE'
代码:
my $sql1 = "SELECT AlertKey,AdditionalText,Identifier FROM alerts.status where AdditionalText like 'priority' AND Summary like 'Uplink' AND Type=1";
my $sth = $dbh->prepare($sql1);
my $alertkey;
my $str;
$sth->execute() || die "SQL Error: couldnt execute $DBI::errstr\n";
while(my @row = $sth->fetchrow_array())
{
print "Inside while\n";
my $str=$row[1];
print "\nAdditional Text=".$str;
$alertkey=$row[0];
print "\nAlert Key before modification=".$alertkey;
my $regex = qr/"link_index":"(\d+)"/mp;
if($str =~ /$regex/g)
{
my $linkIndex=$1;
$alertkey='Link '.$linkIndex.' Circuit Up down';
print "\nAlertKey after modification=".$alertkey;
}
my $sql2 = "UPDATE alerts.status SET AlertKey='$alertkey' WHERE Identifier='$row[2]'";
my $sth1 = $dbh->prepare($sql2);
$sth1->execute() || die "SQL Error: couldnt update $DBI::errstr\n";;
print "Number of rows updated :" + $sth->rows;
$sth1->finish();
$dbh->commit or die $DBI::errstr;
}
$ dbh-> disconnect();
答案 0 :(得分:0)
正如注释中指出的那样,您的代码有几个问题。我不确定以下内容是否可以解决您的问题,但至少它没有提到的任何问题。
顺便说一句:我根据您的正则表达式做出了一个有根据的猜测,即AdditionalText
列实际上包含一个JSON字符串。然后,您应该使用JSON解析器而不是应用正则表达式。
#!/usr/bin/perl
use strict;
use warnings;
use JSON qw(decode_json);
# DB setup etc.
use DBI....
my $dbh = ...
...
# Query for alert status
my $query_sth = $dbh->prepare(
q(SELECT AlertKey,AdditionalText,Identifier FROM alerts.status where AdditionalText like 'priority' AND Summary like 'Uplink' AND Type=1)
);
# Update AlertKey
my $update_sth = $dbh->prepare(
q(UPDATE alerts.status SET AlertKey=? WHERE Identifier=?)
);
# Execute query
$query_sth->execute()
or die "SQL Error: couldn't execute $DBI::errstr\n";
# Loop over results
foreach my $row ($query_sth->fetchrow_arrayref) {
my($key, $text, $id) = @{ $row };
my $data;
# Educated guess: $text is actually a JSON string
eval {
$data = decode_json($text);
};
if ($@) {
# handle JSON parse errors here...
next;
}
my $link = $data->{link_index};
unless ($link) {
# handle missing link index here...
next;
}
# update alert key
my $alert = "Port ${link} Circuit Up down";
$update_sth->execute($alert, $id)
or die "SQL Error: couldn't update $DBI::errstr\n";
}
注意::不不建议这样做。如果您使用JSON,XML,CSV等格式,请执行以下操作:始终使用解析器并在“已解析”域中工作。
my($key, $text, $id) = @{ $row };
# NOTE: regex might be incorrect if JSON property "link_index"
# is actually a number, not a string (see following line)
my($link) = ($text =~ /"link_index":\s*"(\d+)"/;
# = ($text =~ /"link_index":\s*(\d+)/;
unless ($link) {
# handle missing link index here...
next;
}