SQL查询中问号代表什么?
在阅读一些 SQL 书籍时,我发现示例倾向于在查询中使用问号 (?
)。它代表什么?
While going through some SQL books I found that examples tend to use question marks (?
) in their queries. What does it represent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您看到的是参数化查询。从程序执行动态 SQL 时经常使用它们。
例如,不要写这个(注意:伪代码):
你写这个:
这有很多优点,这可能是显而易见的。最重要的之一:解析参数的库函数非常聪明,并确保字符串正确转义。例如,如果您这样写:
当用户输入此内容时会发生什么?
(答案是此处)
改为这样写:
然后库将清理输入,产生这个:
不是所有的 DBMS 都使用
?
。 MS SQL 使用命名参数,我认为这是一个巨大改进:What you are seeing is a parameterized query. They are frequently used when executing dynamic SQL from a program.
For example, instead of writing this (note: pseudocode):
You write this:
This has many advantages, as is probably obvious. One of the most important: the library functions which parse your parameters are clever, and ensure that strings are escaped properly. For example, if you write this:
What happens when the user enters this?
(Answer is here)
Write this instead:
Then the library will sanitize the input, producing this:
Not all DBMS's use
?
. MS SQL uses named parameters, which I consider a huge improvement:?
是一个未命名参数,可以由运行查询的程序填写,以避免 SQL 注入。The
?
is an unnamed parameter which can be filled in by a program running the query to avoid SQL injection.?
用于允许参数化查询。这些参数化查询是在将?
替换为其各自的值时允许特定于类型的值。仅此而已。
有 使用参数化查询是良好实践的几个原因。从本质上讲,它更容易阅读和调试,并且可以规避 SQL 注入攻击。
The
?
is to allow Parameterized Query. These parameterized query is to allow type-specific value when replacing the?
with their respective value.That's all to it.
There are several reasons why it's good practice to use Parameterized Queries. In essence, it's easier to read and debug, and circumvents SQL injection attacks.
这是一个参数。您可以在执行查询时指定它。
It's a parameter. You can specify it when executing query.
我认为这在 SQL 中没有任何意义。您可能正在查看 JDBC 中的预准备语句或其他内容。在这种情况下,问号是语句参数的占位符。
I don't think that has any meaning in SQL. You might be looking at Prepared Statements in JDBC or something. In that case, the question marks are placeholders for parameters to the statement.
它通常表示由客户端提供的参数。
It normally represents a parameter to be supplied by client.