Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如何将postgres dbd占位符用于DB对象名称_Perl_Postgresql - Fatal编程技术网

Perl 如何将postgres dbd占位符用于DB对象名称

Perl 如何将postgres dbd占位符用于DB对象名称,perl,postgresql,Perl,Postgresql,(或者如何使用perl DBI(DBD::PG)和占位符迭代信息模式?) Windows7、ActiveState Perl 5.20.2、PostgreSQL 9.4.1 下面的案例A、B和C在使用占位符作为列值时成功。为了 没有使用占位符 传递一个文本 传递了一个变量(用相同的文本填充) 将其提升到DB对象级别将是非常好的。。(表格、视图等) 下面是案例D的错误输出: Z:\CTAM\data_threat_mapping\DB Stats\perl scripts>test_pl

(或者如何使用perl DBI(DBD::PG)和占位符迭代信息模式?)

Windows7、ActiveState Perl 5.20.2、PostgreSQL 9.4.1

下面的案例A、B和C在使用占位符作为列值时成功。为了

  • 没有使用占位符

  • 传递一个文本

  • 传递了一个变量(用相同的文本填充)

将其提升到DB对象级别将是非常好的。。(表格、视图等)

下面是案例D的错误输出:

Z:\CTAM\data_threat_mapping\DB Stats\perl scripts>test_placeholder.pl

A Row Count: 1
B Row Count: 1
C Row Count: 1

DBD::Pg::st execute failed: ERROR:  syntax error at or near "$1"

LINE 1: SELECT COUNT(*) FROM $1 WHERE status = 'Draft';
                             ^ at Z:\CTAM\data_threat_mapping\DB     Stats\perl 
scripts\test_placeholder.pl line 34.
多谢指点

#!/usr/bin/perl -w
use strict;
use diagnostics;
use DBI;

my $num_rows = 0;

# connect
my $dbh = DBI->connect("DBI:Pg:dbname=CTAM;host=localhost",
                       "postgres", "xxxxx",
                       { 'RaiseError' => 1, pg_server_prepare => 1 });

#---------------------
# A - success
my $sthA = $dbh->prepare(
    "SELECT COUNT(*) FROM cwe_compound_element WHERE status = 'Draft';"
);
$sthA->execute(); # no placeholders

#---------------------
# B -  success
my $sthB = $dbh->prepare (
    "SELECT COUNT(*) FROM cwe_compound_element WHERE status = ?;"
);
$sthB->execute('Draft'); # pass 'Draft' to placeholder

#---------------------
# C -  success
my $status_value = 'Draft';
my $sthC = $dbh->prepare(
    "SELECT COUNT(*) FROM cwe_compound_element WHERE status = ?;"
);
$sthC->execute($status_value); # pass variable (column value) to placeholder

#---------------------
# D - failure
my $sthD = $dbh->prepare(
    "SELECT COUNT(*) FROM ? WHERE status = 'Draft';"
);
$sthD->execute('cwe_compound_element'); # pass tablename to placeholder
我试过单引号/双引号/无引号(q,qq).

如果

SELECT * FROM Foo WHERE field = ?
意味着

SELECT * FROM Foo WHERE field = 'val'
然后

意味着

这显然是错误的。占位符只能在表达式中使用。修正:

my $sthD = $dbh->prepare("
   SELECT COUNT(*)
    FROM ".$dbh->quote_identifier($table)."
   WHERE status = 'Draft'
");
$sthD->execute();
如果

意味着

SELECT * FROM Foo WHERE field = 'val'
然后

意味着

这显然是错误的。占位符只能在表达式中使用。修正:

my $sthD = $dbh->prepare("
   SELECT COUNT(*)
    FROM ".$dbh->quote_identifier($table)."
   WHERE status = 'Draft'
");
$sthD->execute();