如何使用Perl的DBI模块中的数组中的参数选择多行? [英] How can I select multiple rows using parameters from an array in Perl's DBI module?

查看:96
本文介绍了如何使用Perl的DBI模块中的数组中的参数选择多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取与列 name 相对应的行。被拉出的行对应于数组 @values 中的地址。以下是我的代码:

I am required to pull out rows corresponding to column name. The rows being pulled out correspond to address in array @values. Following is my code:

use strict;
use DBI;

open (FH, "/user/address") or die $!;
my@values=<FH>;
close(FH);
my @names;

my $query = "Select name from table where address = ?";
my $sth = $dbh->prepare( $query ) or die "could not prepare statement\n", $dbh->errstr;
foreach my $value(@values){ #@values contain list of address
        $sth->execute($value) or die "could not execute statement $query\n", $sth->errstr;
        while ($result = $sth->fetchrow_hashref()){
               my $name_reqd = $result->{name};
               print "Name Req: $name_reqd\n"; #not printing anything 
               push (@names, $name_reqd);
        }
}
print "@names\n"; #not printing anything

但是当我打印 @names ,我没有任何输出,我不确定出什么问题了。

But when I print @names, I don't get any output, I am unsure as to what is going wrong.

推荐答案

此问题代码是文件读取。代码 my @ values =< FH> ;; 在以下位置读取所有带有换行符( \n )的行结束。在这种情况下,应手动将其删除。您可以使用 chomp 函数:

The problem with this code is file reading. Code my @values=<FH>; reads all lines with new-line (\n) symbol at the end. It should be manually removed in this case. You can do it using chomp function:

open (FH, "/user/address") or die $!;
my @values = <FH>;
chomp(@values);
close(FH);

更新:
我认为它没有搜索任何内容,因为它就是找不到。地址通常在其中包含空格。查询从表中选择名称,其中address =?将仅找到完全相等的地址(字母大小写是唯一被忽略的东西)。例如,在SQL中, a不等于 a。

Update: I think it's not searching anything because it just can't find. Addresses usually have spaces within. Query Select name from table where address = ? will find only exact equal addresses (letter case is the only thing ignored). For example " a" is not equal to "a" in sql.

这篇关于如何使用Perl的DBI模块中的数组中的参数选择多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆