绑定消息提供 2 个参数,但准备语句“”需要 1 个
bind message supplies 2 parameters, but prepared statement "" requires 1
- 我正在使用 pg-promise 执行以下查询
这是原始查询变量
const query = `SELECT * FROM feed_items WHERE feed_item_id= AND '{}' <@ tags`
- tags 是一个数组,我想检查数组中是否存在项目
- 我一直收到此错误绑定消息提供了 2 个参数,但是尽管提供了 2 个值,准备好的语句“”仍需要 1 个
有人可以建议我哪里出错了
Prepared Statements 不会让你这样做,因为它太有限了。但是 pg-promise
本机格式非常灵活,您可以通过多种方式实现...
通过':value' filter,您可以使用'{#}'
或{:value}
通过Custom Type Formatting,可以直接使用</code>,同时将值包装成如下:</p>
</li>
</ul>
<pre><code>const wrap = a => ({rawType: true, toPostgres: () => pgp.as.format('{#}', [a])});
或者像这样:
const wrap = a => ({rawType: true, toPostgres: () => pgp.as.format('{:value}', [a])});
甚至像这样:
const wrap = a => ({rawType: true, toPostgres: () => `'{${pgp.as.value(a)}}'`});
示例:*
await db.any('SELECT ... <@ tags', [wrap(123)]);
//=> SELECT ... '{123}' <@tags'
- 我正在使用 pg-promise 执行以下查询
这是原始查询变量
const query = `SELECT * FROM feed_items WHERE feed_item_id= AND '{}' <@ tags`
- tags 是一个数组,我想检查数组中是否存在项目
- 我一直收到此错误绑定消息提供了 2 个参数,但是尽管提供了 2 个值,准备好的语句“”仍需要 1 个 有人可以建议我哪里出错了
Prepared Statements 不会让你这样做,因为它太有限了。但是 pg-promise
本机格式非常灵活,您可以通过多种方式实现...
通过':value' filter,您可以使用
'{#}'
或{:value}
通过Custom Type Formatting,可以直接使用
</code>,同时将值包装成如下:</p> </li> </ul> <pre><code>const wrap = a => ({rawType: true, toPostgres: () => pgp.as.format('{#}', [a])});
或者像这样:
const wrap = a => ({rawType: true, toPostgres: () => pgp.as.format('{:value}', [a])});
甚至像这样:
const wrap = a => ({rawType: true, toPostgres: () => `'{${pgp.as.value(a)}}'`});
示例:*
await db.any('SELECT ... <@ tags', [wrap(123)]); //=> SELECT ... '{123}' <@tags'