【发布时间】:2020-12-19 05:15:24
【问题描述】:
我正在尝试编写一个 postgres 查询(在 nodejs 中使用使用 node-postgres 包创建的池执行),它将在表中插入一个新行。此表中的一列是text[] 类型。我的代码如下:
pool.query('INSERT INTO paragraphs (lines) VALUES (ARRAY[$1]::TEXT[]) RETURNING id', [my_array], (err, results) => {
if (err) {
reject(err)
return;
}
resolve(results.rows[0].id)
})
paragraphs 是我的表的名称,lines 是 text[] 类型的列的名称。 my_array 是一个字符串列表。我的问题是插入的不是字符串数组,而是像数组一样格式化的单个字符串。例如:
{"[\"First line\", \"Second line\", \"Third Line\"]"}
我希望它是:
{"First line", "Second line", "Third Line"}
我也试过取出ARRAY 和TEXT 部分(所以上面查询中的sql 看起来像INSERT INTO paragraphs (lines) VALUES ($1) RETURNING id),但后来我收到错误:
malformed array literal: "["New First line", "Second line", "Third Line"]"
DETAIL: "[" must introduce explicitly-specified array dimensions.
通过池执行的查询将字符串列表插入nodejs中的PostgreSQL表的正确方法是什么?
【问题讨论】:
标签: node.js arrays postgresql sql-insert node-postgres