简体   繁体   中英

sql: expected 3 destination arguments in Scan, not 1 in Golang

I am writing a generic code to query data from any RDS table. I have gone through many StackOverflow answers but none of it worked for me. I have gone through below links:-

My first code is

package main

import (
    "fmt"
)

type BA_Client struct {
    ClientId    int    `json:ClientId;"`
    CompanyName string `json:CompanyName;"`
    CreateDate  string `json:CreateDate;"`
}

func main() {

    conn, _ := getConnection() // Det database connection

    query := `select * from IMBookingApp.dbo.BA_Client
              ORDER BY
                ClientId ASC
              OFFSET 56 ROWS
              FETCH NEXT 10 ROWS ONLY ;`

    var p []byte
    err := conn.QueryRow(query).Scan(&p)
    if err != nil {
        fmt.Println("Error1", err)
    }

    fmt.Println("Data:", p)

    var m BA_Client
    err = json.Unmarshal(p, &m)
    if err != nil {
        fmt.Println("Error2", err)
    }

}

My second code is

conn, _ := getConnection()
query := `select * from IMBookingApp.dbo.BA_Client__c
          ORDER BY
            ClientId__c ASC
          OFFSET 56 ROWS
          FETCH NEXT 10 ROWS ONLY ;`

rows, err := conn.Query(query)
if err != nil {
    fmt.Println("Error:")
    log.Fatal(err)
}

println("rows", rows)

defer rows.Close()

columns, err := rows.Columns()
fmt.Println("columns", columns)

if err != nil {
    panic(err)
}

for rows.Next() {
    receiver := make([]*string, len(columns))

    err := rows.Scan(&receiver )
    if err != nil {
        fmt.Println("Error reading rows: " + err.Error())
    }
    fmt.Println("Data:", p)

    fmt.Println("receiver", receiver)
}

With both the codes, I am getting the same error as below

sql: expected 3 destination arguments in Scan, not 1

Can it be because I am using SQL Server and not MySQL? Appreciate if anyone can help me to find the issue.

When you want to use a slice of inputs for your row scan, use the variadic 3-dots notation ... to convert the slice into individual parameters, like so:

err := rows.Scan(receiver...)

Your (three in this case) columns, using the variadic arguments will effectively expand like so:

// len(receiver) == 3
err := rows.Scan(receiver[0], receiver[1], receiver[2])

EDIT :

SQL Scan method parameter values must be of type interface{} . So we need an intermediate slice. For example:

is := make([]interface{}, len(receiver))
for i := range is {
    is[i] = receiver[i]

    // each is[i] will be of type interface{} - compatible with Scan()
    // using the underlying concrete `*string` values from `receiver`
}

// ...

err := rows.Scan(is...)

// `receiver` will contain the actual `*string` typed items

try this (no ampersand in front of receiver) for second example:

err := rows.Scan(receiver)

receiver and *string are already a reference types. No need to add another reference to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM