perl DBI 和占位符
我有这个查询 select * from table where ID in (1,2,3,5...)
如何使用占位符通过 DBI 构建此查询?
例如:
my @list = (1, 2, 3, 4, 5);
my $sql = "select * from table where ID in (?)";
$sth->prepare($sql);
$sth->execute();
我应该发送什么参数来执行?它是一个列表还是一个由 、
或其他内容分隔的字符串?
I have this query select * from table where ID in (1,2,3,5...)
How is it possible to build this query with the DBI using placeholders ?
for example :
my @list = (1, 2, 3, 4, 5);
my $sql = "select * from table where ID in (?)";
$sth->prepare($sql);
$sth->execute();
What argument should I send to execute? Is it a list or a string separated by ,
or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我总结了上述所有建议,找到了一种可行的方法。我的生产查询(我在这里发布了一个更简单的版本)使用 IN <>,其中代码及其数量都是未知的。它可以是单个代码(例如
FIN
),也可以是一系列代码(FITLC FITLD FITLU FITSC FITSD FITSU MDYLC MDYLD MDYLU
)。某些函数将其作为列表返回。实现这一点的代码是
结果示例:
Processing Category: RatingChange -->代码: FITLC FITLD FITLU FITSC FITSD FITSU MDYLC MDYLD MDYLU MDYSC MDYSD MDYSU SPLD SPLPR SPLU SPSD SPSPR SPSU .. 中: ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
359496HQ2 359496GB6 359496GH3 359496GL4 359496HU3 359496HS8 359496HA7 359496HF6 359496GM2 359496HM1 359496HR0 359496HT6 359496GY6 359496GJ9 359496HL3 359496GU4 359496HK5 359496HN9 359496HP4 359496GW0 359496GZ3 359496HC3 359496GC4 359496GK6 359496GP5 359496GV2 359496GX8 359496GN0
我非常感谢在这里发表想法的每个人,他们最终让我找到了正确的方法来做到这一点。我想这一定是一个很常见的问题。
I found a sure way for this to work summarizing all of the above advice. My Production query (I posted a much simpler version here) uses IN <>, where neither the codes nor their quantity is unknown. It could be a single Code (e.g.
FIN
), or a series of them (FITLC FITLD FITLU FITSC FITSD FITSU MDYLC MDYLD MDYLU
). Some function returns that as a list.The code that makes this happen is
The result sample:
Processing Category: RatingChange --> Codes: FITLC FITLD FITLU FITSC FITSD FITSU MDYLC MDYLD MDYLU MDYSC MDYSD MDYSU SPLD SPLPR SPLU SPSD SPSPR SPSU .. in: ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
359496HQ2 359496GB6 359496GH3 359496GL4 359496HU3 359496HS8 359496HA7 359496HF6 359496GM2 359496HM1 359496HR0 359496HT6 359496GY6 359496GJ9 359496HL3 359496GU4 359496HK5 359496HN9 359496HP4 359496GW0 359496GZ3 359496HC3 359496GC4 359496GK6 359496GP5 359496GV2 359496GX8 359496GN0
I'm extremely grateful to everybody who posted their ideas here that finally made me find the right way to do this. It must be a pretty common problem I think.
这应该根据数组中的项目数动态构建您的查询
This should build your query dynamically according to the number of items in your array
那样的话是不可能的。您需要为数组中的每个项目指定一个占位符:
如果您的
@list
不是固定大小,则需要使用适当数量的占位符构建$sql
。It's not possible in that way. You need to specify a placeholder for each item in your array:
If your
@list
is not a fixed size, you need to build the$sql
with the proper number of placeholders.引用
DBI
文档:重写为:
Quoting
DBI
documentation:Rewrite to:
如果您使用 DBI 通过 DBD::Pg 驱动程序访问 PostgreSQL 数据库,则可以使用:
If you are using DBI to access a PostgreSQL database with the DBD::Pg driver, you can use:
除非您知道元素的确切数量,否则不能使用占位符。试试这个:
Unless you know the exact number of elements you cannot use placeholders. Try this:
如果您切换到 DBIx::Simple 您可以直接说:
??意思是“需要多少就多少”
编辑:
实际上,我有点太乐观了:“如果查询中存在字符串 (??),它将被替换为 as 的列表许多问号为@values。”
所以这似乎不起作用:
但仍然有用......
对于好奇的人来说,模块中的代码是:
If you switch to DBIx::Simple you can just say:
?? Means "as many as needed"
Edit:
Actually, I was a little too optimistic: "If the string (??) is present in the query, it is replaced with a list of as many question marks as @values."
So this does not seem to work:
Still useful though..
For the curious, the code in the module is: