sql:预期 1 个参数,但得到了 2 个。

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

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

huangapple
  • 本文由 发表于 2021年9月13日 07:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/69155805.html
匿名

发表评论

匿名网友

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

确定