dongwei9771 2013-03-06 14:10
浏览 69

将空接口转换为字符串

I'm using the mymysql package and I'm trying to create a function which gets an SQL query and some parameters (as variadic empty interface):

func FindByQuery(statement string, params ...interface{}) (diver *DiverT, err error) {
    values := make([]interface{}, len(params))
    for i := range params {
            values[i] = params[i]
    }

    // Both statements result in the same error...
    row, _, execError := Db.QueryFirst(statement,values...)

    row, _, execError := Db.QueryFirst(statement,params...)

    // Additional code...
}

When I call this method using some kind of SQL, I always get an SQL error. I do something like:

FindByQuery("SELECT * FROM Diver WHERE Name=?", "Markus")

Which results in the following error:

Received #1064 error from MySQL server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?%!(EXTRA string=Markus)' at line 1"

What should I do that the parameter is converted correctly to a string (or whatever it is, if I have different parameter(s))?

  • 写回答

1条回答 默认 最新

  • duanbai1027 2014-03-25 13:03
    关注

    Try using printf format syntax:

    FindByQuery("SELECT * FROM Diver WHERE Name=%s", "Markus")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集