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

问题描述 投票:-1回答:1

我尝试使用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->断开();

perl dbi
1个回答
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的替代方案

注意:不建议这样做。如果您有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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.