Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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';正确使用DBI_Perl_Informix_Dbi_Dbaccess - Fatal编程技术网

如何在Perl';正确使用DBI

如何在Perl';正确使用DBI,perl,informix,dbi,dbaccess,Perl,Informix,Dbi,Dbaccess,我在Informix表中有一条记录。表列如下所示: acct_no integer, suffix char(1), meter_num char(20), date_read datetime year to second not null , counter smallint, reading integer not null , typeofread char(1), estimated char(1), time_billed datetime year to second 使用Inf

我在Informix表中有一条记录。表列如下所示:

acct_no integer,
suffix char(1),
meter_num char(20),
date_read datetime year to second not null ,
counter smallint,
reading integer not null ,
typeofread char(1),
estimated char(1),
time_billed datetime year to second
使用Informix的dbaccess工具:

select * 
from ws_mtrread 
where acct_no = 113091000 
and suffix = " " 
order by date_read desc;
无论我是否使用一个或两个空格作为后缀,都会返回这个结果(显示的最新结果)

acct_no      113091000
suffix
meter_num    34153205
date_read    2013-09-09 23:31:15
counter      0
reading      1240
typeofread   g
estimated
time_billed  2013-10-22 11:48:21
但是,这个Perl DBI查询

my $sql_statement = 
"select * ".
"from ws_mtrread ".
"where acct_no = ? ".
"and suffix = ? ".
"order by date_read desc ; ";
不起作用。我可以在不指定$suffix的情况下获取该行,因此我知道该行存在

我相信这是我在表示后缀时的一个错误。在本例中,后缀等于由两个空格组成的字符串

如何正确表示空间,以便查询工作?下面是我用来获取行的其余代码

my $test_acct_no = 113091000;
my $suffix = "  ";

my $pt_sel_hdl = $DBHdl->prepare($sql_statement);

$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));

my $ws_mtr_read_rec_ref = $pt_sel_hdl->fetchrow_hashref;

调用后,$ws\u mtr\u read\u rec\u ref未定义。

此处不要使用DBI的
引用方法:

$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
当您在SQL中使用
占位符时,数据库驱动程序将正确地参数化传递给
execute
的查询参数。当您要搜索
(只有两个空格)时,您可能正在创建一个搜索文本字符串
“”
(包括引号)的查询

因此,这应该是您所需要的全部:

$pt_sel_hdl->execute($test_acct_no, $suffix);

不要在此处使用DBI的
quote
方法:

$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
当您在SQL中使用
占位符时,数据库驱动程序将正确地参数化传递给
execute
的查询参数。当您要搜索
(只有两个空格)时,您可能正在创建一个搜索文本字符串
“”
(包括引号)的查询

因此,这应该是您所需要的全部:

$pt_sel_hdl->execute($test_acct_no, $suffix);

我试试看,但有个问题。Informix接受分号来终止字符串。为什么会有问题?@Barmar这是Informix,不是MySQL。我不知道如何表示errstr()。如果我像你所列出的那样使用,它将是未定义的。我会尝试,但这里有一个问题。Informix接受分号来终止字符串。为什么会有问题?@Barmar这是Informix,不是MySQL。我不知道如何表示errstr()。如果我像你所列的那样使用它,它将是未定义的。谢谢。这很有帮助,对。quote()用于不使用占位符,并且希望用引号将参数直接插入SQL语句中的情况。@runrig我正在慢慢地将所有$SQL\u语句变量更改为使用占位符。Informix 4GL使用嵌入式值或占位符,因此我习惯了两种方法。我通常在4GL中使用带占位符的准备好的语句。谢谢。这很有帮助,对。quote()用于不使用占位符,并且希望用引号将参数直接插入SQL语句中的情况。@runrig我正在慢慢地将所有$SQL\u语句变量更改为使用占位符。Informix 4GL使用嵌入式值或占位符,因此我习惯了两种方法。我通常在4GL中使用带占位符的准备语句。