dousong9729 2017-09-01 11:21
浏览 23
已采纳

命名查询无法解析地图

I have this code segement

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
    }

When runnig this snippet, I get an error like this:

Error getting co credentials for co 'some.random.fqdn': could not find name in map[string]interface {}{"co_url":"some.random.fqdn"}

But if I rework the snippet to be

in.Url = "https://" + in.Url

query := `
            SELECT
                co_username as username,
                co_password as password
            FROM
                servers.co
            WHERE
                co_url = :co_url
        `

Everything works. Is there a reason the named input is not working with the Concat function?

  • 写回答

1条回答 默认 最新

  • dongzuan4860 2017-09-01 15:24
    关注

    This would depend on what sql library you are using, but when using NamedQuery in many libraries, you must escape any other colons, which you will find how to do in its documentation. For example, sqlx requires you to escape a colon with two e.g.

    co_url = concat('https:://', :co_url)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败