[英]How to insert multiple rows using node-postgres?
This is my statement: 这是我的声明:
INSERT INTO userpermissions (username, permission)
VALUES ($1, $2),($3,$4)
RETURNING *;
This is my code: 这是我的代码:
db.query(stmt2,values2, (err2, result2) => {
//Do other stuff depending on if there is an error or result
}
Where stmt2, is the above statement, and values2 is an array which I create by doing: 其中stmt2是上面的语句,而values2是我通过执行以下操作创建的数组:
var values2 = [];
for(i=0;i<permissions.length;i++){
values2.push(username);
values2.push(permissions[i]);
}
There is some kind of error / fatal exception, but I can't see it because the domain of the error is wrapped. 有某种错误/致命异常,但我看不到它,因为错误的域已包装。
sql: SQL:
f=# create table tt(i int,t text);
CREATE TABLE
f=# grant all on tt to t;
GRANT
js: JS:
const { Pool, Client } = require('pg')
const client = new Client({
user: 't',
host: '10.10.10.10',
database: 'f',
password: 't',
port: 5432
})
client.connect()
client.query('INSERT INTO tt(i, t) VALUES($1, $2),($3,$4) RETURNING *', ['1', 'SO',2,'sO'], (err, res) => {
console.log(err, res)
client.end()
})
run: 跑:
C:\Users\Vao\vatest>node t.js
null Result {
command: 'INSERT',
rowCount: 2,
oid: 0,
rows: [ anonymous { i: 1, t: 'SO' }, anonymous { i: 2, t: 'sO' } ],
fields:
[ Field {
name: 'i',
tableID: 131471390,
columnID: 1,
dataTypeID: 23,
dataTypeSize: 4,
dataTypeModifier: -1,
format: 'text' },
Field {
name: 't',
tableID: 131471390,
columnID: 2,
dataTypeID: 25,
dataTypeSize: -1,
dataTypeModifier: -1,
format: 'text' } ],
_parsers: [ [Function: parseInteger], [Function: noParse] ],
RowCtor: [Function: anonymous],
rowAsArray: false,
_getTypeParser: [Function: bound ] }
finally check: 最后检查:
f=# select * from tt;
i | t
---+----
1 | SO
2 | sO
(2 rows)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.