简体   繁体   English

Perl-DBI-如何处理数组而不是单个值?

[英]Perl - DBI - How to process array rather than single value?

I have code running a SQL query select rec_data from foo where id=? 我有运行SQL查询的代码, select rec_data from foo where id=? , and I do something with the return value. ,然后使用返回值进行处理。

I now have an array of IDs @queryIDs; 我现在有一个ID @queryIDs;数组@queryIDs; , how can I run the same query and gather the results in order for the values in this array? ,如何运行相同的查询并收集结果以按此数组中的值排序? Thanks 谢谢

Something like this could work: 这样的事情可能会起作用:

my $sql = 'select rec_data from foo where id in (';
$sql .= join ',', ("?") x @queryIDs;
$sql .= ') order by id';

my $sth = $dbh->prepare($sql);

$sth-> execute(@queryIDs);

# rest of code will be the same as you have now

Dynamic SQL is insecure. 动态SQL是不安全的。

To do the same securely, split the list in SQL, which depends on the RDBMS and version. 为了安全地执行此操作,请在SQL中拆分列表,具体取决于RDBMS和版本。 For example, Oracle 11r2 has a good tokenization method via XMLDB which can be found with a simple search, or the MODEL clause, for those ot scared to use it. 例如,Oracle 11r2通过XMLDB有一个很好的令牌化方法,对于那些害怕使用它的人,可以通过简单的搜索或MODEL子句找到它。 SQL Server also does XML. SQL Server也支持XML。 Without the RDBMS and version, it is hard to suggest what to do. 没有RDBMS和版本,很难建议该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM