postgresql Golang pq:执行sql时语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21073650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:16:00  来源:igfitidea点击:

Golang pq: syntax error when executing sql

sqlpostgresqlgorevel

提问by Derek

Using revel, golang 1.1.2, gorp, postgres 9.3.2on heroku

使用revel, golang 1.1.2, gorp, postgres 9.3.2onheroku

Following robfig's List booking example

以下robfig是列表预订示例

func (c App) ViewPosts(page int) revel.Result {
    if page == 0 {
        page = 1
    }
    var posts []*models.Post
    size := 10
    posts = loadPosts(c.Txn.Select(models.Post{},
        `select * from posts offset ? limit ?`, (page-1)*size, size)) // error here
    return c.RenderJson(posts)
}

Not sure why I'm getting pq: syntax error at or near "limit". I'm assuming the combined query is wrong. Why does the query not end up being something like select * from posts offset 0 limit 10, which I've tested to run on postgres. Where am I messing up?

不知道为什么我得到pq: syntax error at or near "limit". 我假设组合查询是错误的。为什么查询最终不会像select * from posts offset 0 limit 10我测试过的那样在 postgres 上运行。我哪里搞砸了?

回答by mraron

I'm not familiar with postgres, but I found this issue. I think you should use it like in the godoc

我不熟悉 postgres,但我发现了这个问题。我认为你应该像在godoc 中那样使用它

Example in godoc

godoc 中的示例

age := 21
rows, err := db.Query("SELECT name FROM users WHERE age = ", age)

(Replace "?" with "$n")

(将“?”替换为“$n”)

Your code

你的代码

func (c App) ViewPosts(page int) revel.Result {
if page == 0 {
    page = 1
}
var posts []*models.Post
size := 10
posts = loadPosts(c.Txn.Select(models.Post{},
    `select * from posts offset  limit `, (page-1)*size, size))
return c.RenderJson(posts)
}