How can I use a query with placeholder inside quotes? (perl / postgresql)

前端 未结 3 1234
情书的邮戳
情书的邮戳 2020-12-21 00:03

I\'m trying to execute the following script:

#!/usr/bin/perl -w

use strict;
use DBI;

my $db = \"Pg\";
my $db_database = \"whatever\";
my $user = \"whatever         


        
相关标签:
3条回答
  • 2020-12-21 00:24

    I just found a solution here: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=321917

    It works using ?::interval instead of 'interval ?':

    my $query = $dbh->prepare (q{SELECT
                       arrival_date - ?::interval
                       FROM emails LIMIT 1});
    
    $query->execute('60 MINUTE');
    
    0 讨论(0)
  • 2020-12-21 00:35
    my $query = $dbh->prepare (<<SQL) or die ("unable to prepare");
    SELECT arrival_date - INTERVAL ? FROM emails LIMIT 1
    SQL
    $query->execute("60 MINUTE");
    

    or

    my $query = $dbh->prepare (<<SQL) or die ("unable to prepare");
    SELECT arrival_date - INTERVAL CONCAT(?, " MINUTE") FROM emails LIMIT 1
    SQL
    $query->execute(60);
    

    0 讨论(0)
  • 2020-12-21 00:36

    You can't use placeholders inside quotes. You can use SQL string concatenation, but in this case, it's easier to do it by using multiplication:

    my $query = $dbh->prepare (q{SELECT
                       arrival_date - ? * INTERVAL '1 MINUTE'
                       FROM emails LIMIT 1});
    $query->execute(60);
    

    That way, you don't have to append ' minutes' to the number when you execute the query.

    0 讨论(0)
提交回复
热议问题