go build error "rows.Columns undefined (type *sql.Row has no field or method Columns)"

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

go build error "rows.Columns undefined (type *sql.Row has no field or method Columns)"

问题

我想使用Golang从我的PostgreSQL数据库中打印多行多列的数据。在构建以下代码时,我遇到了一些错误:

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. _ "github.com/lib/pq"
  7. "log"
  8. "runtime"
  9. )
  10. func main() {
  11. runtime.GOMAXPROCS(runtime.NumCPU())
  12. db, err := sql.Open("postgres", "dbname=sample_data user=postgres password=postgres sslmode=disable")
  13. defer db.Close()
  14. if err != nil {
  15. fmt.Println("error connecting to DB")
  16. }
  17. r := gin.Default()
  18. r.GET("/cin_display", func(c *gin.Context) {
  19. rows := db.QueryRow("SELECT cin FROM companies limit 1;")
  20. columns, _ := rows.Columns()
  21. count := len(columns)
  22. values := make([]interface{}, count)
  23. valuePtrs := make([]interface{}, count)
  24. for rows.Next() {
  25. for i, _ := range columns {
  26. valuePtrs[i] = &values[i]
  27. }
  28. rows.Scan(valuePtrs...)
  29. for i, col := range columns {
  30. var v interface{}
  31. val := values[i]
  32. b, ok := val.([]byte)
  33. if ok {
  34. v = string(b)
  35. } else {
  36. v = val
  37. }
  38. fmt.Println(col, v)
  39. }
  40. }
  41. })
  42. }
  43. func Connect(connectionString string) *sql.DB {
  44. db, err := sql.Open("postgres", connectionString)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. return db
  49. }

我得到了以下错误:

  • rows.Columns 未定义(*sql.Row 类型没有 Columns 字段或方法)
  • rows.Next 未定义(*sql.Row 类型没有 Next 字段或方法)

如何解决这个问题?

英文:

I want to print multiple rows having multiple columns from my postgresql database using golang.while buildng the following code

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. _ "github.com/lib/pq"
  7. "log"
  8. "runtime"
  9. )
  10. func main() {
  11. runtime.GOMAXPROCS(runtime.NumCPU())
  12. db, err := sql.Open("postgres", "dbname=sample_data user=postgres password=postgres sslmode=disable")
  13. defer db.Close()
  14. if err != nil {
  15. fmt.Println("error connecting to DB")
  16. }
  17. r := gin.Default()
  18. r.GET("/cin_display", func(c *gin.Context) {
  19. rows := db.QueryRow("SELECT cin FROM companies limit 1;")
  20. columns, _ := rows.Columns()
  21. count := len(columns)
  22. values := make([]interface{}, count)
  23. valuePtrs := make([]interface{}, count)
  24. for rows.Next() {
  25. for i, _ := range columns {
  26. valuePtrs[i] = &values[i]
  27. }
  28. rows.Scan(valuePtrs...)
  29. for i, col := range columns {
  30. var v interface{}
  31. val := values[i]
  32. b, ok := val.([]byte)
  33. if ok {
  34. v = string(b)
  35. } else {
  36. v = val
  37. }
  38. fmt.Println(col, v)
  39. }
  40. }
  41. })
  42. }
  43. func Connect(connectionString string) *sql.DB {
  44. db, err := sql.Open("postgres", connectionString)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. return db
  49. }

I am getting the errors like

> rows.Columns undefined (type *sql.Row has no field or method Columns)
>
> rows.Next undefined (type *sql.Row has no field or method Next)

how to solve this?

答案1

得分: 2

QueryRow 返回一个单独的 *sql.Row

你需要的是 Query,它会返回一个 *sql.Rows,该类型具有你想要使用的方法。

英文:

QueryRow returns a single *sql.Row

What you want is Query, which will give you *sql.Rows which has the methods you are trying to use.

答案2

得分: 1

你在这里屏蔽了database/sql包:sql := `select * from table
sql现在是一个字符串,而不是包。将该字符串重命名为query,它就会起作用。

英文:

you masked the database/sql package here: sql := `select * from table
sql is then a string, not the package. Rename the string e.g. to query and it will work

huangapple
  • 本文由 发表于 2015年10月16日 21:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/33171661.html
匿名

发表评论

匿名网友

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

确定