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

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

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

问题

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

package main

import (
    "database/sql"
    "fmt"
    "github.com/gin-gonic/gin"
    _ "github.com/lib/pq"
    "log"
    "runtime"
)

func main() {

    runtime.GOMAXPROCS(runtime.NumCPU())

    db, err := sql.Open("postgres", "dbname=sample_data user=postgres password=postgres sslmode=disable")
    defer db.Close()
    if err != nil {
        fmt.Println("error connecting to DB")
    }
    r := gin.Default()
    r.GET("/cin_display", func(c *gin.Context) {
        rows := db.QueryRow("SELECT cin FROM companies limit 1;")

        columns, _ := rows.Columns()
        count := len(columns)
        values := make([]interface{}, count)
        valuePtrs := make([]interface{}, count)

        for rows.Next() {

            for i, _ := range columns {
                valuePtrs[i] = &values[i]
            }

            rows.Scan(valuePtrs...)

            for i, col := range columns {

                var v interface{}

                val := values[i]

                b, ok := val.([]byte)

                if ok {
                    v = string(b)
                } else {
                    v = val
                }

                fmt.Println(col, v)
            }
        }

    })
}

func Connect(connectionString string) *sql.DB {
    db, err := sql.Open("postgres", connectionString)
    if err != nil {
        log.Fatal(err)
    }
    return db
}

我得到了以下错误:

  • 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

package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"log"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
db, err := sql.Open("postgres", "dbname=sample_data user=postgres password=postgres sslmode=disable")
defer db.Close()
if err != nil {
fmt.Println("error connecting to DB")
}
r := gin.Default()
r.GET("/cin_display", func(c *gin.Context) {
rows := db.QueryRow("SELECT cin FROM companies limit 1;")
columns, _ := rows.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for rows.Next() {
for i, _ := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
for i, col := range columns {
var v interface{}
val := values[i]
b, ok := val.([]byte)
if ok {
v = string(b)
} else {
v = val
}
fmt.Println(col, v)
}
}
})
}
func Connect(connectionString string) *sql.DB {
db, err := sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
return db
}

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:

确定