postgres:get executable query from query with parameters

北城以北 提交于 2019-12-04 20:24:37

pg-promise has a query formatting engine that can be used independently of queries.

const pgp = require('pg-promise')(/* Initialization Options */);

const query = pgp.as.format('INSERT INTO table(column1,column2,column3) VALUES($1,$2,$3)',
    ["data1", "data2", "data3"]);

console.log(query);

Outputs:

INSERT INTO table(column1,column2,column3) VALUES('data1','data2','data3')

See format API.

UPDATE

For a high-performance approach with a single INSERT query see Multi-row insert with pg-promise.

Kaasi Gopalakrishnan

You might want to create a procedure in the PostgreSQL which accepts the query name and arguments as parameters, the procedure can prepare the query you are passing in and execute with the parameters. There is an example of how to do this in MySQL, please find below.

How To have Dynamic SQL in MySQL Stored Procedure

http://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!