为什么我的 SQL 占位符没有被替换(使用 Go pq)?

huangapple go评论125阅读模式
英文:

Why aren't my SQL placeholders being replaced (using Go pq)?

问题

根据文档,我正在执行以下操作:

var thingname string = "asdf";
var id int
err = database.QueryRow("SELECT id from things where thing = ?", thingname).Scan(&id)

但是Postgres报错:

ERROR:  syntax error at end of input at character 41
STATEMENT:  SELECT id from things where thing = ?

我看不出我和演示代码有什么不同。我正在使用pq

英文:

As per the docs, I'm doing this

var thingname string = "asdf";
var id int
err = database.QueryRow("SELECT id from things where thing = ?", thingname).Scan(&id)

but Postgres is saying

ERROR:  syntax error at end of input at character 41
STATEMENT:  SELECT id from things where thing = ?

I can't see that I'm doing much different to the demo code. I'm using pq.

答案1

得分: 4

确切的语法取决于数据库。使用以下代码:

err = database.QueryRow("SELECT id from things where thing = $1", thingname).Scan(&id)
英文:

The exact syntax is database dependent.

Use

err = database.QueryRow("SELECT id from things where thing = $1", thingname).Scan(&id)

答案2

得分: 2

请尝试使用$1替代?来进行查询:

err = database.QueryRow("SELECT id from things where thing = $1", thingname).Scan(&id)
英文:

Try this using $1 instead of ?:-

err = database.QueryRow("SELECT id from things where thing = $1", thingname).Scan(&id)

huangapple
  • 本文由 发表于 2013年9月3日 00:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/18577855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定