如果不存在,则对 INSERT INTO 进行 Node-postgres 参数化查询
Node-postgres parameterized query for INSERT INTO if not exists
我有一个用于插入语句的有效参数化查询,但我现在想添加一个 'WHERE NOT EXISTS' 子句。工作插入看起来像这样:
pgClient.query("INSERT INTO social_posts (username, user_image, message, image_url, post_id, post_url, location, network) VALUES (,,,,,,,)", postArray,
function(err, result) {...}
我想实现的是:
pgClient.query("INSERT INTO social_posts(username, user_image, message, image_url, post_id, post_url, location, network) SELECT (,,,,,,,) WHERE NOT EXISTS (SELECT 1 FROM social_posts WHERE post_id = )", 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 ]
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' }
有人有什么想法吗?提前致谢!
经过一些研究,我发现了一些有用的东西 here。我无法让 $9 参数工作,因为它不能与插值一起工作的原因与 SELECT 项周围的一组额外括号有关。正确的语法是:
var postID = postArray[4].toString() // per joop's comment above
pgClient.query("INSERT INTO social_posts(username, user_image, message, image_url, post_id, post_url, location, network) SELECT ,,,,,,, WHERE NOT EXISTS (SELECT 1 FROM social_posts WHERE post_id = '" + postId + "')", postArray,
function(err, result) {...}
我有一个用于插入语句的有效参数化查询,但我现在想添加一个 'WHERE NOT EXISTS' 子句。工作插入看起来像这样:
pgClient.query("INSERT INTO social_posts (username, user_image, message, image_url, post_id, post_url, location, network) VALUES (,,,,,,,)", postArray,
function(err, result) {...}
我想实现的是:
pgClient.query("INSERT INTO social_posts(username, user_image, message, image_url, post_id, post_url, location, network) SELECT (,,,,,,,) WHERE NOT EXISTS (SELECT 1 FROM social_posts WHERE post_id = )", 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 ]
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' }
有人有什么想法吗?提前致谢!
经过一些研究,我发现了一些有用的东西 here。我无法让 $9 参数工作,因为它不能与插值一起工作的原因与 SELECT 项周围的一组额外括号有关。正确的语法是:
var postID = postArray[4].toString() // per joop's comment above
pgClient.query("INSERT INTO social_posts(username, user_image, message, image_url, post_id, post_url, location, network) SELECT ,,,,,,, WHERE NOT EXISTS (SELECT 1 FROM social_posts WHERE post_id = '" + postId + "')", postArray,
function(err, result) {...}