英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论