INSERT INTO的Node-postgres参数化查询(如果不存在)

时间:2015-08-31 16:49:53

标签: node.js postgresql node-postgres

我有一个插入语句的工作参数化查询,但我现在想要添加'WHERE NOT EXISTS'子句。工作插件如下所示:

pgClient.query("INSERT INTO social_posts (username, user_image, message, image_url, post_id, post_url, location, network) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)", postArray, 
    function(err, result) {...}

我想要实现的是:

pgClient.query("INSERT INTO social_posts(username, user_image, message, image_url, post_id, post_url, location, network) SELECT ($1,$2,$3,$4,$5,$6,$7,$8) WHERE NOT EXISTS (SELECT 1 FROM social_posts WHERE post_id = $9)", postArray,
    function(err, result) {...}

在这种情况下,$ 9实际上等于postArray [4]。

我已经尝试将值再次推送到数组中,但这不起作用:

error running query { [error: operator does not exist: character varying = bigint]
name: 'error',
length: 207,
severity: 'ERROR',
code: '42883',
detail: undefined,
hint: 'No operator matches the given name and argument type(s). You might need to add explicit type casts.',
position: '198',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_oper.c',
line: '722',
routine: 'op_error' }

我尝试插入值,但不起作用:

error running query { [error: there is no parameter $1]
name: 'error',
length: 88,
severity: 'ERROR',
code: '42P02',
detail: undefined,
hint: undefined,
position: '114',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_expr.c',
line: '823',
routine: 'transformParamRef' }

有没有人有任何想法?提前谢谢!

1 个答案:

答案 0 :(得分:0)

在做了一些研究后,我发现了一些有用的东西here。我无法获得9美元的参数,因为它不能与interpoloation一起工作,这与SELECT项目周围的一组额外括号有关。正确的语法是:

WriteableBitmap