dousonghs58612 2015-10-16 13:24
浏览 252
已采纳

生成错误“ rows.Columns未定义(类型* sql.Row没有字段或方法列)”

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?

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 使用DWY100k数据集对UEA进行测试,出现报错:IndexError: index 125000 is out of bounds for axis 0 with size 95500
      • ¥15 前端vue实现根据图片url生成pdf文件
      • ¥15 RfidReader资源Q个
      • ¥20 user-agent是否是唯一的,有没有可能相同
      • ¥15 关于#开会#的问题,如何解决?(语言-c++)
      • ¥15 关于#二十四点问题#的问题
      • ¥15 运行kitex的demon出错(求大家解决)
      • ¥15 开发一个类似 typora 这样的软件需要多少钱?
      • ¥15 clearcase7.0安装包
      • ¥15 断点回归模型月度核密度检验不连续