perl - DBD::Pg::st 执行失败:错误:列 "***"不存在

标签 perl postgresql dbi

那是我的代码的一部分。我使用从 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

最佳答案

$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 .

关于perl - DBD::Pg::st 执行失败:错误:列 "***"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25863770/

相关文章:

perl - 写入文件时奇怪地显示西里尔符号

sql - 无法让这个 PostgreSQL 查询更好地工作

oracle - 破折号导致 DBI 中的 SQL 问题

Perl:带有 DATE_FORMAT 的 DBI SQL 错误

perl - 如何计算 Perl 中出现的奇数次?

perl - 我如何使用 Perl 在 AGI 中转发当前的 Asterisk 调用?

perl - 如何在 Perl 中禁用自动生存?

postgresql - 如何在 Spring Boot 中使用 NoSql Postgres

python - Heroku + Django migrate 命令不会创建任何表

mysql - 在连接 (.) 中使用未初始化值 $oldvalue 或 perl 中的字符串错误?