SQL查询中问号代表什么?

发布于 2024-09-24 01:45:08 字数 61 浏览 4 评论 0原文

在阅读一些 SQL 书籍时,我发现示例倾向于在查询中使用问号 (?)。它代表什么?

While going through some SQL books I found that examples tend to use question marks (?) in their queries. What does it represent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

儭儭莪哋寶赑 2024-10-01 01:45:08

您看到的是参数化查询。从程序执行动态 SQL 时经常使用它们。

例如,不要写这个(注意:伪代码):

ODBCCommand cmd = new ODBCCommand("SELECT thingA FROM tableA WHERE thingB = 7")
result = cmd.Execute()

你写这个:

ODBCCommand cmd = new ODBCCommand("SELECT thingA FROM tableA WHERE thingB = ?")
cmd.Parameters.Add(7)
result = cmd.Execute()

这有很多优点,这可能是显而易见的。最重要的之一:解析参数的库函数非常聪明,并确保字符串正确转义。例如,如果您这样写:

string s = getStudentName()
cmd.CommandText = "SELECT * FROM students WHERE (name = '" + s + "')"
cmd.Execute()

当用户输入此内容时会发生什么?

Robert'); DROP TABLE students; --

(答案是此处

改为这样写:

s = getStudentName()
cmd.CommandText = "SELECT * FROM students WHERE name = ?"
cmd.Parameters.Add(s)
cmd.Execute()

然后库将清理输入,产生这个:

"SELECT * FROM students where name = 'Robert''); DROP TABLE students; --'"

不是所有的 DBMS 都使用 。 MS SQL 使用命名参数,我认为这是一个巨大改进:

cmd.Text = "SELECT thingA FROM tableA WHERE thingB = @varname"
cmd.Parameters.AddWithValue("@varname", 7)
result = cmd.Execute()

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):

ODBCCommand cmd = new ODBCCommand("SELECT thingA FROM tableA WHERE thingB = 7")
result = cmd.Execute()

You write this:

ODBCCommand cmd = new ODBCCommand("SELECT thingA FROM tableA WHERE thingB = ?")
cmd.Parameters.Add(7)
result = cmd.Execute()

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:

string s = getStudentName()
cmd.CommandText = "SELECT * FROM students WHERE (name = '" + s + "')"
cmd.Execute()

What happens when the user enters this?

Robert'); DROP TABLE students; --

(Answer is here)

Write this instead:

s = getStudentName()
cmd.CommandText = "SELECT * FROM students WHERE name = ?"
cmd.Parameters.Add(s)
cmd.Execute()

Then the library will sanitize the input, producing this:

"SELECT * FROM students where name = 'Robert''); DROP TABLE students; --'"

Not all DBMS's use ?. MS SQL uses named parameters, which I consider a huge improvement:

cmd.Text = "SELECT thingA FROM tableA WHERE thingB = @varname"
cmd.Parameters.AddWithValue("@varname", 7)
result = cmd.Execute()
咽泪装欢 2024-10-01 01:45:08

? 是一个未命名参数,可以由运行查询的程序填写,以避免 SQL 注入

The ? is an unnamed parameter which can be filled in by a program running the query to avoid SQL injection.

何以心动 2024-10-01 01:45:08

? 用于允许参数化查询。这些参数化查询是在将 ? 替换为其各自的值时允许特定于类型的值。

仅此而已。

使用参数化查询是良好实践的几个原因。从本质上讲,它更容易阅读和调试,并且可以规避 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.

另类 2024-10-01 01:45:08

这是一个参数。您可以在执行查询时指定它。

It's a parameter. You can specify it when executing query.

晨光如昨 2024-10-01 01:45:08

我认为这在 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.

冷了相思 2024-10-01 01:45:08

它通常表示由客户端提供的参数。

It normally represents a parameter to be supplied by client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文