英文:
How to escape back ticks
问题
MySQL要求使用反引号(back ticks)来标记与保留字相同的表名。我有一个名为"Role"的表,但我已经在查询中使用了反引号,以便可以将其写在多行上(这只是一个示例查询,实际的查询可能不适合一行)。
那么,如何转义反引号呢?
以下是我的代码:
dbmap := db.InitDb()
var roles []entities.Role
query :=
`<<Difficult to see with SO's code editor widget, but here is a back tick
SELECT *
FROM \`Role\` <<< Needs escaping
` <<Difficult to see, but here is a back tick
_, err := dbmap.Select(&roles, query, nil)
if err != nil {
panic(err)
}
fmt.Println(roles)
英文:
MySQL requires tables that shadow reserved words to be back ticked. I have a table Role which is a reserved word, but I have already put my query in back ticks so I can write it over multiple lines (this is a toy query, large ones will not fit on one line).
How do I escape the back ticks?
Here is my code:
dbmap := db.InitDb()
var roles []entities.Role
query :=
` << Difficult to see with SO's code editor widget, but here is a back tick
SELECT *
FROM `Role` <<< Needs escaping
` << Difficult to see, but here is a back tick
_, err := dbmap.Select(&roles, query, nil)
if err != nil {
panic(err)
}
fmt.Println(roles)
答案1
得分: 107
你无法在反引号内部使用反引号,但你可以这样做:
dbmap := db.InitDb()
var roles []entities.Role
query := `
SELECT *
FROM ` + "`Role`"
_, err := dbmap.Select(&roles, query, nil)
if err != nil {
panic(err)
}
fmt.Println(roles)
英文:
You cannot escape backticks inside backticks, but you can do:
dbmap := db.InitDb()
var roles []entities.Role
query := `
SELECT *
FROM ` + "`Role`"
_, err := dbmap.Select(&roles, query, nil)
if err != nil {
panic(err)
}
fmt.Println(roles)
答案2
得分: 6
你可以使用 .
前缀:
query := `
SELECT *
FROM .Role
`
英文:
You can use .
prefix:
query := `
SELECT *
FROM .Role
`
答案3
得分: 5
如果你的查询很长,最好将其放入一个文本文件中并进行读取,这样可以使你的代码更简洁和有组织,并且完全避免反引号引用的问题。
英文:
If your query is long, it might be worth putting in a text file and reading it in, that will keep your code more concise and organized, and also avoid the backtick quoting issue entirely.
答案4
得分: 5
我刚刚使用了一个占位符(比如Unicode的”
或者基本上在查询中不会出现的任何字符)来代替反引号,然后再进行替换:
strings.ReplaceAll(`CREATE TABLE ”mydatabase”.”mytable” (
”id” binary(16),
”sname” varchar(45),
PRIMARY KEY(”id”)
)`, "”", "`")
(感谢Andrey Tarantsov提出使用”
作为占位符。)
英文:
I just used a placeholder (like the unicode ”
or basically anything which won't appear anywhere else in the query) instead of the backticks and replaced them afterwards:
strings.ReplaceAll(`CREATE TABLE ”mydatabase”.”mytable” (
”id” binary(16),
”sname” varchar(45),
PRIMARY KEY(”id”)
)`, "”", "`")
(Thanks to Andrey Tarantsov for proposing ”
as a placeholder.)
答案5
得分: 4
使用记事本++对纯文本进行替换(搜索和)替换
`
替换为
`+"`"+`
英文:
Use notepad++ on your plain text and replace (search and) replace
`
with
`+"`"+`
答案6
得分: 2
你可以尝试这样编写查询语句:
query := fmt.Sprintf("SELECT * FROM `Role`")
你可以比较输出结果:
import "fmt"
func main() {
query := fmt.Sprintf("SELECT * FROM `Role`")
fmt.Println(query)
fmt.Println(`SELECT * FROM ` + "`Role`")
}
英文:
You can try writing queries like this:
query :=fmt.Sprintf("SELECT * FROM `Role`")
You can compare the outputs:
import "fmt"
func main() {
query :=fmt.Sprintf("SELECT * FROM `Role`")
fmt.Println(query)
fmt.Println( `SELECT * FROM ` + "`Role`") }
答案7
得分: 1
如果你正在使用Go模板,你可以将反引号作为参数传递进去:
package main
import (
"fmt"
"text/template"
"bytes"
)
func main() {
template,_ := template.New("greeting").Parse(`Hello {{ .BT }}{{ .FirstName }}{{ .BT }}`)
data := struct {
FirstName string
BT string
}{
FirstName:"bob",
BT:"`", // <---- 这里!
}
var buf bytes.Buffer
_ = template.Execute(&buf, data)
fmt.Print(buf.String())
}
输出结果为:
Hello `bob`
英文:
If you are using Go Templates you can pass the backtick in as a parameter:
package main
import (
"fmt"
"text/template"
"bytes"
)
func main() {
template,_ := template.New( "greeting").Parse(`Hello {{ .BT }}{{ .FirstName }}{{ .BT }}`)
data := struct {
FirstName string
BT string
}{
FirstName:"bob",
BT:"`", // <---- Here!
}
var buf bytes.Buffer
_ = template.Execute(&buf, data)
fmt.Print(buf.String())
}
gives:
Hello `bob`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论