【发布时间】:2020-11-19 20:13:57
【问题描述】:
我正在使用包含运算符 @> 查询数组列 (style text[])。我的原始查询是:
SELECT * FROM songs WHERE style @> '{Acoustic}'
当我将它放入节点(使用pg-pool)并尝试对其进行参数化时,值引用周围的大括号似乎阻止它正常工作。请注意以下示例:
const style = 'Acoustic';
// this works, but I don't want to in-line like this:
const { rows } = await pool.query(`SELECT * FROM songs WHERE style @> '{` + style + `}'`);
// this fails with 'error: bind message supplies 1 parameters, but prepared statement "" requires 0'
const { rows } = await pool.query(`SELECT * FROM songs WHERE style @> '{$1}'`, [style]);
参数化的正确方法是什么?
【问题讨论】:
-
请注意,我使用另一种查询:
pool.query('SELECT * FROM songs WHERE $1 = ANY (style)', [style]);,但我想知道是否有办法使用上面的表单。
标签: node.js postgresql pgpool node-pg-pool