问题描述
为了检测 hstore 中键的存在,我需要运行一个查询像这样:
For detecting the existence of a key in a hstore, I need to run a query like this:
SELECT * FROM tbl WHERE hst ? 'foo'
然而,这给了我一个 PDOException:
However, that gives me a PDOException:
PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound: SELECT * FROM tbl WHERE hst ? 'foo'
有什么办法可以避开问号,这样 PDO 就不会把它当作占位符?我试过最多四个反斜杠,以及一个双问号 (??
),但似乎没有什么能说服 PDO 单独留下问号.
Is there any way to escape the question mark so PDO won't pick it up as a placeholder? I've tried with up to four backslashes, as well as a double question mark (??
), but nothing seems to persuade PDO to leave the question mark alone.
推荐答案
使用函数调用形式.根据系统目录,hstore ?
操作符使用了 exist
函数:
Use the function call form. According to the system catalogs, the hstore ?
operator uses the exist
function:
regress=# select oprname, oprcode from pg_operator where oprname = '?';
oprname | oprcode
---------+---------
? | exist
(1 row)
所以你可以写:
SELECT * FROM tbl WHERE exist(hst,'foo');
(就我个人而言,我不是 hstore 以操作员为中心的设计和文档的忠实粉丝,我认为它丢弃了基于函数的接口的有用的自文档属性而没有任何真正的好处,我通常使用它的函数调用而不是它的运算符.仅仅因为您可以定义运算符并不意味着您应该.)
(Personally I'm not a big fan of hstore's operator-centric design and documentation, I think it discards the useful self-documenting properties of a function based interface without any real benefit and I usually use its function calls rather than its operators. Just because you can define operators doesn't mean you should.)
这篇关于如何防止 PDO 将问号解释为占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!