绑定消息提供 3 个参数,但准备语句“”需要 0 [Node js,pg]
bind message supplies 3 parameters, but prepared statement "" requires 0 [Node js, pg]
我想像这样将参数传递给我的 pg 查询:
await client.query("DO\n" +
"$do$\n" +
"DECLARE\n" +
" _db TEXT := ;\n" +
" _user TEXT := ;\n" +
" _password TEXT := ;\n" +
"BEGIN\n" +
" CREATE EXTENSION IF NOT EXISTS dblink; -- enable extension \n" +
" IF EXISTS (SELECT 1 FROM pg_database WHERE datname = _db) THEN\n" +
" RAISE NOTICE 'Database already exists';\n" +
" ELSE\n" +
" PERFORM dblink_connect('host=localhost user=' || _user || ' password=' || _password || ' dbname=' || current_database());\n" +
" PERFORM dblink_exec('CREATE DATABASE ' || _db);\n" +
" END IF;\n" +
"END\n" +
"$do$", [process.env.database, process.env.user, process.env.password]);
但我得到 bind message supplies 3 parameters, but prepared statement "" requires 0
。
如果我不传递参数数组,我会得到 there is no parameter
错误。
那么如何将参数传递给我的查询?
根据文档 here,最重要的问题是匿名函数 DO
不接受参数。这意味着提供的参数 [process.env.database, process.env.user, process.env.password]
无处可去,因为 $* 占位符被埋在函数中。
我想像这样将参数传递给我的 pg 查询:
await client.query("DO\n" +
"$do$\n" +
"DECLARE\n" +
" _db TEXT := ;\n" +
" _user TEXT := ;\n" +
" _password TEXT := ;\n" +
"BEGIN\n" +
" CREATE EXTENSION IF NOT EXISTS dblink; -- enable extension \n" +
" IF EXISTS (SELECT 1 FROM pg_database WHERE datname = _db) THEN\n" +
" RAISE NOTICE 'Database already exists';\n" +
" ELSE\n" +
" PERFORM dblink_connect('host=localhost user=' || _user || ' password=' || _password || ' dbname=' || current_database());\n" +
" PERFORM dblink_exec('CREATE DATABASE ' || _db);\n" +
" END IF;\n" +
"END\n" +
"$do$", [process.env.database, process.env.user, process.env.password]);
但我得到 bind message supplies 3 parameters, but prepared statement "" requires 0
。
如果我不传递参数数组,我会得到 there is no parameter
错误。
那么如何将参数传递给我的查询?
根据文档 here,最重要的问题是匿名函数 DO
不接受参数。这意味着提供的参数 [process.env.database, process.env.user, process.env.password]
无处可去,因为 $* 占位符被埋在函数中。