在参数数组中传递一个整数数组 [英] Pass an array of integers in array of parameters

查看:117
本文介绍了在参数数组中传递一个整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在pg-promise的参数数组中传递参数数组,如 pg-promise docs

I am trying to pass an array of parameters in pg-promise's array of parameters, as recommended in pg-promise docs.

db.any("SELECT fieldname FROM table WHERE fieldname = $1 AND fieldname2 IN ($2)",
        [1,[[1730442],[1695256]],[487413],[454336]]])
    .then(function (data) {
        console.log("DATA:", data); // print data;
    })
    .catch(); 

但它不起作用,我在参数列表后返回缺失错误。
或运算符不存在:整数=整数[]]错误,如果我将参数替换为:

But it doesn't work, I'm returned a "missing ) after argument list" error. Or an "operator does not exist: integer = integer[]]" error, if I replace the parameters by :

[1,[1730442]]

当然,如果我这样传递它,它的工作原理如下:

Of course if I pass it like this, it works :

[1,1730442]

当涉及其他参数时,这是传递值数组的正确方法吗?

Is it the proper way of passing an array of values when other parameters are involved?

我还尝试删除$ 2周围的括号,没有成功。

I also tried to remove the parenthesis around the $2, without success.

推荐答案

我是 pg-promise

有一些混乱在你的例子中......

There is some confusion in your example...

你在查询中只使用了两个变量,但传递了四个值:

You are using only two variables in the query, but passing in four values:


  • 1

  • [[1730442],[1695256]]

  • [487413]

  • [454336]

  • 1
  • [[1730442],[1695256]]
  • [487413]
  • [454336]

你的语法没有有效的JavaScript,因为你最后使用] 而没有匹配的开头,所以你很难理解你正在尝试什么传入。

And your syntax there isn't a valid JavaScript, as you are using ] in the end without the matching opening one, so it is hard to understand what it is exactly you are trying to pass in.

然后为什么要再次将所有值包装在数组中?我相信它只是你想要的 IN()语句中的整数列表。

And then why wrap all values in arrays again? I believe it is just a list of integers that you want inside the IN() statement.

当你想要的时候要使用 WHERE IN()中的值,它实际上不是您要传入的值的数组,而是以逗号分隔的值列表。

When you want to use values within WHERE IN(), it is not really an array of those values that you want to pass in, it is a comma-separated list of values.

如果您将示例更改为以下内容:

If you change your example to the following:

db.any('SELECT fieldname FROM table WHERE fieldname = $1 AND fieldname2 IN ($2:csv)',
[1, [1730442,1695256,487413,454336]])

您将获得正确的值列表。

You will get the correct list of values injected.

参见:

  • CSV Filter
  • WHERE col IN example.

这篇关于在参数数组中传递一个整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆