【问题标题】:DBD::Pg::st execute failed: ERROR: column "***" does not existDBD::Pg::st 执行失败:错误:列“***”不存在
【发布时间】:2014-11-09 21:47:15
【问题描述】:

这是我的代码的一部分。我使用从 gmail 收到的消息 ID 进行选择。 msg_id以base64的形式存储在数据库中,不带符号“”。

my $inbox = $imap->select("Inbox") or die "Select error: ", $imap->LastError, "\n";
my @mails = ( $imap->unseen );
foreach my $msgid (@mails) {
    my $m_id = $imap->parse_headers( $msgid, "Message-id" )->{"Message-id"}->[0] . "\n";
    $m_id =~ s/[<\>]//g;
    $m_id = encode_base64($m_id);
    $m_id =~ s/\r?$//;
    my $q1 = "select id from mails.mails_in where user_id=$param[5] and message_id=$m_id and user_remote_id=$param[6]";
    $sth = $dbh->prepare($q1);
    $rv  = $sth->execute();
    my @array;

    while ( @array = $sth->fetchrow_array() ) {
        foreach my $i (@array) {
            print "$i\t";
        }
        print "\n";
    }
}

但出现此错误。

DBD::Pg::st execute failed: ERROR:  column "zdjkot..." does not exist
LINE 1: ...mails.mails_in where user_id=15206 and message_id=ZDJkOTQ1NT...
                                                             ^ at ./script.pl line 65.

我尝试使用现有的 msg_id,从基础 - 结果是相似的。 另一个 SELECT 的工作正常。 类似的 SELECT 在 php 上正常工作。

我使用: Perl v5.18.2、PostgreSQL v8.4.14

【问题讨论】:

    标签: perl postgresql dbi


    【解决方案1】:

    $m_id 缺少单引号

    my $q1 = "select id from mails.mails_in where user_id=$param[5] and message_id='$m_id' and user_remote_id=$param[6]";
    

    但最好使用? 占位符,

    my $q1 = "select id from mails.mails_in where user_id =? and message_id =? and user_remote_id =?";
    $sth = $dbh->prepare($q1);
    $rv = $sth->execute($param[5], $m_id, $param[6]);
    

    因为您不必担心引号、参数转义或SQL injection attacks

    【讨论】:

    • 让我明确强调一个事实:使用占位符不仅更好,而且是唯一正确的方法!!!
    • 感谢您的建议
    猜你喜欢
    • 2013-05-19
    • 2020-06-01
    • 1970-01-01
    • 2023-04-11
    • 2019-07-07
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多