Question:
题:
Is there a way to use names instead of question marks for paramaterized queries? If so, can anyone suggest some material that explains how to do this/the syntax?
对于参数化查询,有没有办法使用名称而不是问号?如果是这样,有人可以建议一些解释如何执行此操作/语法的材料吗?
A bit more detail:
更详细一点:
For example, if I have something like:
例如,如果我有类似的东西:
INSERT INTO inventory VALUES(?)
Is it possible to have something like this instead that does the exact same thing as the question mark:
是否有可能有这样的东西而不是与问号完全相同的东西:
INSERT INTO inventory VALUES("prices")
I tried checking to see if it would work myself before posting the question, but it didn't work. So, I thought I'd ask if it was possible.
在发布问题之前,我试着检查一下它是否能正常工作,但它没有用。所以,我想我会问是否有可能。
I feel like if you have a really long query with, let's say 20 parameters, you don't want to have to count question marks to make sure you have enough parameters whenever you change something. Also, I think it might make the code a bit more readable (especially if you have a lot of parameters to keep track of).
我觉得如果你有一个很长的查询,比方说20个参数,你不想计算问号,以确保每当你改变一些东西时你有足够的参数。此外,我认为它可能使代码更具可读性(特别是如果你有很多参数需要跟踪)。
I'm rather new to sql, so I am not sure if it makes much of a difference (for this question) if I add that I'm using postgresql.
我对sql很新,所以如果我添加我使用postgresql,我不确定它是否会产生很大的影响(对于这个问题)。
Note:
注意:
There is a similar question here, but it didn't have an answer that was helpful
这里有一个类似的问题,但它没有一个有用的答案
1 个解决方案
#1
0
I suggest to encapsulate the big query in a function, where you can use parameter names. One example (out of many):
我建议将大查询封装在一个函数中,您可以在其中使用参数名称。一个例子(在众多中):
- PostgreSQL parameterized Order By / Limit in table function
- PostgreSQL参数化表函数中的Order By / Limit
You can even set default values and call the function with named parameters, etc.:
您甚至可以设置默认值并使用命名参数等调用该函数:
- Functions with variable number of input parameters
- 具有可变数量输入参数的函数
#1
0
I suggest to encapsulate the big query in a function, where you can use parameter names. One example (out of many):
我建议将大查询封装在一个函数中,您可以在其中使用参数名称。一个例子(在众多中):
- PostgreSQL parameterized Order By / Limit in table function
- PostgreSQL参数化表函数中的Order By / Limit
You can even set default values and call the function with named parameters, etc.:
您甚至可以设置默认值并使用命名参数等调用该函数:
- Functions with variable number of input parameters
- 具有可变数量输入参数的函数