使用数组中的 2 个不同参数在 PostgreSQL 中插入记录
Inserting records in PostgreSQL with 2 varying parameters from an array
我正在尝试从数组批量插入 postgresql。我目前正在做的是循环内的以下内容。
INSERT INTO table_name (
first, second, third, forth
) VALUES (
, , ,
)
还有大约 8 到 9 个其他字段,每次迭代中只有 2 个更改。理想情况下,我想做的是与此伪代码
类似的事情
FOREACH (obj IN )
INSERT INTO table_name (
first, second, third, forth
) VALUES (
obj.fieldOne, , obj.fieldThree,
)
并且只提供 </code> 和 <code>
(不变的属性),以及 </code> 这是一个变化的属性数组,像这样 </p>
<p><code>[{one: 123, two: 63}]
我知道 PostgreSQL 允许在一个 INSERT 语句中插入多个值,但是构建查询和管理其参数可能会很麻烦,老实说,我 运行 只是因为参数和查询而远离它字符串地狱。
INSERT INTO tabel_name (
first, second, third, forth
) VALUES (
, , ,
) VALUES (
, , ,
) VALUES (
, , ,
)
我还读到 PostgreSQL 有像 FOR 和 FOREACH 这样的循环,但我不知道在这种情况下如何使用它们。如果相关,我将 NodeJS 与 pg-promise
一起使用。
任何帮助表示赞赏!
使用与 相同的方法,您有两个选择:
- 将其他固定的7个属性添加到数组中的每个对象;
或
- 对仅在开始时设置的列使用预定义值
第一个选项的解决方案很明显,您只需遍历数组并为 7 个属性设置相同的值。
对于第二个选项,您可以使用 Column 的 属性 init
将值重定向到静态对象:
/* You can set this once, since you do not need to change it; */
const staticValues = {
one: 111, three: 333, five: 555, six: 666, seven: 777, eight: 888, nine: 999
};
然后你可以这样定义你的 ColumnSet
对象:
/* helper for defining our static-value column: */
const col = name => ({name, init: ()=> staticValues[name]});
/* Our ColumnSet object: */
const cs = new pgp.helpers.ColumnSet([
col('one'),
'two',
col('three'),
'four',
col('five'),
col('six'),
col('seven'),
col('eight'),
col('nine')],
{table: 'table_name'});
现在当你想要生成一个完整的插入查询时,你将所有固定值设置到你的 staticValues
对象中,然后生成查询:
const query = pgp.helpers.insert(data, cs);
这将根据您最初的想法生成查询字符串,您可以将其传递 data
,其中的每个对象只设置 two
和 four
属性。
然后就可以执行查询了:
db.none(query).then().catch();
Several of my columns depend on more than one parameter. How would I do that?
您可以随意定义此类列,因为类型 Column 非常灵活:
{
name: 'col-name',
init: c => pgp.as.format('myFunction(${one}, ${two}, ${three})', c.source),
mod: ':raw'
}
或使用任何其他格式模板。
我正在尝试从数组批量插入 postgresql。我目前正在做的是循环内的以下内容。
INSERT INTO table_name (
first, second, third, forth
) VALUES (
, , ,
)
还有大约 8 到 9 个其他字段,每次迭代中只有 2 个更改。理想情况下,我想做的是与此伪代码
类似的事情FOREACH (obj IN )
INSERT INTO table_name (
first, second, third, forth
) VALUES (
obj.fieldOne, , obj.fieldThree,
)
并且只提供 </code> 和 <code>
(不变的属性),以及 </code> 这是一个变化的属性数组,像这样 </p>
<p><code>[{one: 123, two: 63}]
我知道 PostgreSQL 允许在一个 INSERT 语句中插入多个值,但是构建查询和管理其参数可能会很麻烦,老实说,我 运行 只是因为参数和查询而远离它字符串地狱。
INSERT INTO tabel_name (
first, second, third, forth
) VALUES (
, , ,
) VALUES (
, , ,
) VALUES (
, , ,
)
我还读到 PostgreSQL 有像 FOR 和 FOREACH 这样的循环,但我不知道在这种情况下如何使用它们。如果相关,我将 NodeJS 与 pg-promise
一起使用。
任何帮助表示赞赏!
使用与
- 将其他固定的7个属性添加到数组中的每个对象;
或
- 对仅在开始时设置的列使用预定义值
第一个选项的解决方案很明显,您只需遍历数组并为 7 个属性设置相同的值。
对于第二个选项,您可以使用 Column 的 属性 init
将值重定向到静态对象:
/* You can set this once, since you do not need to change it; */
const staticValues = {
one: 111, three: 333, five: 555, six: 666, seven: 777, eight: 888, nine: 999
};
然后你可以这样定义你的 ColumnSet
对象:
/* helper for defining our static-value column: */
const col = name => ({name, init: ()=> staticValues[name]});
/* Our ColumnSet object: */
const cs = new pgp.helpers.ColumnSet([
col('one'),
'two',
col('three'),
'four',
col('five'),
col('six'),
col('seven'),
col('eight'),
col('nine')],
{table: 'table_name'});
现在当你想要生成一个完整的插入查询时,你将所有固定值设置到你的 staticValues
对象中,然后生成查询:
const query = pgp.helpers.insert(data, cs);
这将根据您最初的想法生成查询字符串,您可以将其传递 data
,其中的每个对象只设置 two
和 four
属性。
然后就可以执行查询了:
db.none(query).then().catch();
Several of my columns depend on more than one parameter. How would I do that?
您可以随意定义此类列,因为类型 Column 非常灵活:
{
name: 'col-name',
init: c => pgp.as.format('myFunction(${one}, ${two}, ${three})', c.source),
mod: ':raw'
}
或使用任何其他格式模板。