我有 sql 查询需要变量替换才能更好地消耗我的go-kit https://github.com/go-kit/kit服务。
I have dep
& org
作为我的休息服务一部分的用户输入,例如:dep = 'abc'
and org = 'def'
.
我尝试过一些事情,例如:
rows, err := db.Query(
"select name from table where department='&dep' and organisation='&org'",
)
And:
rows, err := db.Query(
"select name from table where department=? and organisation=?", dep , org,
)
这导致了错误:sql: statement expects 0 inputs; got 2
只有硬编码值才有效,而替换会失败。
我没有从 Oracle 博客中找到关于此问题的太多帮助,并且想知道是否有任何方法可以解决此问题。
参数占位符语法(参考:http://go-database-sql.org/prepared.html http://go-database-sql.org/prepared.html )
准备好的语句中占位符参数的语法是
数据库特定的。例如,比较 MySQL、PostgreSQL 和
甲骨文:
MySQL PostgreSQL Oracle
===== ========== ======
WHERE col = ? WHERE col = $1 WHERE col = :col
VALUES(?, ?, ?) VALUES($1, $2, $3) VALUES(:val1, :val2, :val3)
对于 Oracle,您需要使用 :dep、:org 作为占位符。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)