gpt4 book ai didi

node.js - node-postgres:如何在不执行查询的情况下准备语句?

转载 作者:搜寻专家 更新时间:2023-10-31 22:27:31 26 4
gpt4 key购买 nike

我想使用 node-postgres 模块在 postgres 中创建一个“准备好的语句”。我想在不将其绑定(bind)到参数的情况下创建它,因为绑定(bind)将在循环中进行。

documentation我读了:

query(object config, optional function callback) : Query
If _text_ and _name_ are provided within the config, the query will result in the creation of a prepared statement.

我试过了

client.query({"name":"mystatement", "text":"select id from mytable where id=$1"});

但是当我尝试只传递配置对象中的文本和名称键时,我得到一个异常:

(已翻译)消息绑定(bind)了 0 个参数,但准备好的语句需要 1 个

有什么我想念的吗?如何在不将语句绑定(bind)到特定值的情况下创建/准备语句,以避免在循环的每个步骤中重新准备语句?

最佳答案

我刚找到一个 answer on this issue由 node-postgres 的作者编写。

With node-postgres the first time you issue a named query it is parsed, bound, and executed all at once. Every subsequent query issued on the same connection with the same name will automatically skip the "parse" step and only rebind and execute the already planned query.

Currently node-postgres does not support a way to create a named, prepared query and not execute the query. This feature is supported within libpq and the client/server protocol (used by the pure javascript bindings), but I've not directly exposed it in the API. I thought it would add complexity to the API without any real benefit. Since named statements are bound to the client in which they are created, if the client is disconnected and reconnected or a different client is returned from the client pool, the named statement will no longer work (it requires a re-parsing).

关于node.js - node-postgres:如何在不执行查询的情况下准备语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12331668/

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