gpt4 book ai didi

node-postgres - 在 node-postgres 中使用字符串文字进行参数化查询

转载 作者:行者123 更新时间:2023-12-04 16:46:51 30 4
gpt4 key购买 nike

我正在尝试编写一个 node-postgres 查询,它采用一个整数作为参数在间隔中使用:

  const query = {
text:
`SELECT
foo
FROM bar
WHERE
DATE(created_at) >= DATE(NOW()) - INTERVAL '$1 DAYS';`,
values: [daysAgo]
}

当我运行它时,它给出了一条错误消息,表明它没有看到 $1,因为它用单引号括起来了:

bind message supplies 1 parameters, but prepared statement "" requires 0

是否有支持的方法来执行此操作?如果不是,最好的解决方法是什么?

最佳答案

我找到了 this reference这表明您可以使用 '1 DAY' * X 的约定。

我是这样工作的:

   (async () => {
const { rows } = await pgpool.query(`
SELECT id, name FROM users
WHERE DATE(created) <= DATE(NOW()) - INTERVAL '1 DAY' * $1;`,
[req.params.daysAgo]);

关于node-postgres - 在 node-postgres 中使用字符串文字进行参数化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47257396/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com