我通过perl更新查询时出现解析错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我通过perl更新查询时出现解析错误相关的知识,希望对你有一定的参考价值。

我尝试使用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
";

while(my @row = $sth->fetchrow_array())
{
        print "Inside while
";
        my $str=$row[1];
        print "
Additional Text=".$str;
        $alertkey=$row[0];
        print "
Alert 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 "
AlertKey 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
";;
        print "Number of rows updated :" + $sth->rows;
        $sth1->finish();
        $dbh->commit or die $DBI::errstr;
 }

$ dbh->断开();

答案

正如评论中指出的,您的代码有几个问题。我不确定以下内容是否能解决您的问题,但至少它没有任何上述问题。

顺便说一句:我根据你的正则表达式做了一个有根据的猜测,即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
";

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

不使用JSON的替代方案

注意:不建议这样做。如果您有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;
    }

以上是关于我通过perl更新查询时出现解析错误的主要内容,如果未能解决你的问题,请参考以下文章

在 python 代码中执行更新查询时出现此错误

暂存应用程序.... 解析包时出现问题

运行查询时出现 BigQuery 错误“解析从位置开始的行时检测到错误:219019。错误:缺少右双引号 (”) 字符

通过 Terraform 运行“aws stepfunctions update-state-machine”时出现 Json 解析错误

安装 Perl 模块时出现“致命错误:找不到‘EXTERN.h’文件”

RODBCext:使用 sqlExecute() 时出现 SQL 42000 402 错误