执行sql时Golang pq:语法错误

时间:2014-01-12 10:36:10

标签: sql postgresql go revel

revel上使用golang 1.1.2gorppostgres 9.3.2heroku

关注robfig的列表booking example

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)
}

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

1 个答案:

答案 0 :(得分:11)

我对postgres不熟悉,但我发现了issue。我认为你应该像godoc

一样使用它

godoc中的示例

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

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

您的代码

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 $1 limit $2`, (page-1)*size, size))
return c.RenderJson(posts)
}