英文:
sql: expected 1 arguments, got 2
问题
我正在尝试执行查询,但似乎无法正常工作,出现了一个"sql: 期望1个参数,但提供了2个"的错误。
return func(c echo.Context) error {
rows, err := db.Query("SELECT * FROM posts ORDER BY id DESC")
if err != nil {
panic(err)
}
defer rows.Close()
result := PostCollection{}
for rows.Next() {
post := Post{}
err2 := rows.Scan(&post.ID, &post.Fullname, &post.Content)
if err2 != nil {
panic(err2)
}
result.Posts = append(result.Posts, post)
}
return c.JSON(http.StatusOK, result)
}
}
英文:
I am trying to execute queries here, but doesn't seem to work with always a sql: expected 1 arguments, got 2 error
return func(c echo.Context) error {
rows, err := db.Query("SELECT * FROM posts ORDER BY id DESC")
if err != nil {
panic(err)
}
defer rows.Close()
result := PostCollection{}
for rows.Next() {
post := Post{}
err2 := rows.Scan(&post.ID, &post.Fullname, &post.Content)
if err2 != nil {
panic(err2)
}
result.Posts = append(result.Posts, post)
}
return c.JSON(http.StatusOK, result)
}
}
答案1
得分: 1
如果你只想查询ID、FullName和Content,可以尝试以下代码:
db.Query("SELECT ID, FullName, Content FROM posts ORDER BY id DESC")
这段代码将按照ID的降序查询posts表中的ID、FullName和Content字段。
英文:
if your just want query ID, FullName, Content
cloud try this
db.Query("SELECT ID, FullName, Content FROM posts ORDER BY id DESC")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论