我有这段代码
query := `
SELECT
co_username as username,
co_password as password
FROM
servers.co
WHERE
co_url = concat('https://', :co_url)
`
args := map[string]interface{}{
"co_url": in.Url,
}
rows, err := collectorsConfig.Db.NamedQueryContext(ctx, query, args)
if err != nil {
msg := "Error getting co credentials for co '%s': %v"
log.Error.Printf(msg, in.Url, err)
return
}
当运行此代码段时,我收到如下错误:
获取co' some.random.fqdn'的co凭据时出错:找不到 地图中的名称[string] interface {} {" co_url":" some.random.fqdn"}
但是,如果我将代码片段改写为
in.Url = "https://" + in.Url
query := `
SELECT
co_username as username,
co_password as password
FROM
servers.co
WHERE
co_url = :co_url
`
一切正常。命名输入是否与Concat
函数无关?
答案 0 :(得分:0)
这取决于您使用的sql
库,但在许多库中使用NamedQuery
时,必须转义任何其他冒号,您将找到如何在其文档中做。例如,sqlx
要求您使用两个例如
co_url = concat('https:://', :co_url)