在Go语言中编写文本SQL查询的正确方法是什么?

huangapple go评论80阅读模式
英文:

Correct way to write a text SQL query in Go

问题

我找不到一个好的示例来演示如何正确地将文本查询的字符串部分与值连接起来。例如:

query := `SELECT column_name FROM table_name
        WHERE column1_name = %d AND column2_name = %d` % (val1, val2)
rows, res, err := db.Query(query)

这段代码是不起作用的。编译器会返回 syntax error: unexpected comma, expecting ) 错误,可能是因为我试图使用 Python 风格的元组。

如果我将其重写为:

query := `SELECT column_name FROM table_name
        WHERE column1_name = %d AND column2_name = %d` % val1

我会得到 (mismatched types string and int) 错误,这告诉我元组是其中一个问题。

如果我先将参数转换为字符串,我会得到 (operator % not defined on string) 错误。

在 Python 中,你可以这样做:

query = """SELECT column_name FROM table_name
    WHERE column1_name = %d
    AND column2_name = %d""" % (val1, val2)

或者

query = """SELECT column_name FROM table_name
    WHERE column1_name = %s
    AND column2_name = %s""" % (val1_string, val2_string)

我知道我可以将值转换为字符串,然后使用 "STRING" + var + "STRING" 进行连接,但与 Python 版本相比,这种方法看起来很混乱。在 Go 中,相当于 Python 代码的等价物是什么?特别是包括元组部分,并且连接一个字符串和一个整数。

英文:

I can't find a good example of the right way to concat the string portion of a text query with the values. For example:

query := `SELECT column_name FROM table_name
        WHERE column1_name = %d AND column2_name = %d` % (val1, val2)
rows, res, err := db.Query(query)

This doesn't work. The compiler returns syntax error: unexpected comma, expecting )
Likely because I'm trying to use a python style tuple.

If I rewrite it as

query := `SELECT column_name FROM table_name
        WHERE column1_name = %d AND column2_name = %d` % val1

I get (mismatched types string and int) which tells me that the tuple was ONE OF the problems.

If I cast my parameters as strings first, I get (operator % not defined on string)

In python, you'd do something like

query = """SELECT column_name FROM table_name
    WHERE column1_name = %d
    AND column2_name = %d""" % (val1, val2)

OR

query = """SELECT column_name FROM table_name
    WHERE column1_name = %s
    AND column2_name = %s""" % (val1_string, val2_string)

I know I could just cast the values as strings and concat with "STRING" + var + "STRING", but that seems really messy compared to the python version. What's the equivalent of that python code in Go? Specifically including the tuple portion, and concatenating a string and an integer.

答案1

得分: 8

以下是翻译好的内容:

<关于在 SQL 语句中使用字符串插值的标准警告,因为存在注入漏洞的风险>

你可以使用 fmt.Sprintf 来处理这个问题。

query := fmt.Sprintf(`SELECT columnA FROM tableA WHERE columnB = %d AND columnB = %s`,
                     someNumber, someString)

为了避免注入问题,将你的第一个代码写成:

query := `SELECT column_name FROM table_name
    WHERE column1_name = %d AND column2_name = %d`

rows, err := db.Query(query, val1, val2)
英文:

< standard admonishment about using string interpolation with SQL statements because of injection vulnerabilities >

You can use fmt.Sprintf to handle this.

query := fmt.Sprintf(`SELECT columnA FROM tableA WHERE columnB = %d AND columnB = %s`,
                     someNumber, someString)

To avoid injection issues, write your first code as:

query := `SELECT column_name FROM table_name
    WHERE column1_name = %d AND column2_name = %d`

rows, err := db.Query(query, val1, val2)

huangapple
  • 本文由 发表于 2016年3月20日 13:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/36110601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定