postgres:从带有参数的查询中获取可执行查询 [英] postgres:get executable query from query with parameters
本文介绍了postgres:从带有参数的查询中获取可执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从带有$参数的查询中获取可执行查询。实际上它很奇怪,但是我想将可执行查询存储在数据库中。没有参数的完整查询($ 1,$ 2,$ 3)
Is there any way to get executable query from query with $ parameters.Actually its weird but i want to store executable query in database.A complete query without parameters($1,$2,$3)
我正在使用 node-postgres
pg.connect(conString, function(err, client, done) {
console.log('Executing Insert query');
client.query('insert into tablename(column1,column2,column3) values($1,$2,$3)',["data1","data2","data3"], function(err, result) {
done();
console.log('finished executing Insert query');
});
});
这就是我需要的
insert into tablename(column1,column2,column3) values("data1","data2","data3")
推荐答案
pg -promise 具有查询格式化引擎,可以独立于查询使用。
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);
输出:
插入表(column1,column2,column3)VALUES('data1','data2','data3')
请参见< a href = http://vitaly-t.github.io/pg-promise/formatting.html#.format rel = nofollow noreferrer>格式API 。
更新
使用单个 INSERT $ c $的高性能方法c>查询请参见带有pg-promise的多行插入。
这篇关于postgres:从带有参数的查询中获取可执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文