javascript - 如何使用 node-postgres 将多行正确插入 PG?

标签 javascript sql node.js postgresql node-postgres

可以像这样插入一行:

client.query("insert into tableName (name, email) values ($1, $2) ", ['john', 'john@gmail.com'], callBack)

这种方法会自动注释掉任何特殊字符。

如何一次插入多行?

我需要实现这个:

"insert into tableName (name, email) values ('john', 'john@gmail.com'), ('jane', 'jane@gmail.com')"

我可以使用 js 字符串运算符手动编译这些行,但是我需要以某种方式添加特殊字符转义。

最佳答案

使用 pg-format如下所示。

var format = require('pg-format');

var values = [
  [7, 'john22', 'john22@gmail.com', '9999999922'], 
  [6, 'testvk', 'testvk@gmail.com', '88888888888']
];
client.query(format('INSERT INTO users (id, name, email, phone) VALUES %L', values),[], (err, result)=>{
  console.log(err);
  console.log(result);
});

关于javascript - 如何使用 node-postgres 将多行正确插入 PG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34990186/

相关文章:

javascript - 即使用户正在导航到其他页面,也会在一段时间后打开弹出窗口

sql - 根据其他 2 个表更新第 3 个表列

javascript - 使用下拉列表从数据库中检索数据 PHP/MySQL/HTML

javascript - 用 bool 注解修饰 JS 对象

node.js - 在集合插入之前等待完成 Meteor.call

javascript - Ember Router - 确定路由/资源是否是当前路由的子路由

javascript - jQuery JavaScript 编码 HTML

c# - 将其转换为 SQL 的最佳方法是什么

javascript - 将 Node.js 模块中的 readline 函数集成到 readline 函数中以获取用户输入

javascript - ECMAScript 2017 中的 CallExpression 与 NewExpression