Rahul Satal
Rahul Satal

Reputation: 2237

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.

Upvotes: 2

Views: 8853

Answers (3)

Deni Al Farizi
Deni Al Farizi

Reputation: 2453

specific field table

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

change to

query := `select ClientId, CompanyName, CreateDate   from IMBookingApp.dbo.BA_Client__c
      ORDER BY
        ClientId__c ASC
      OFFSET 56 ROWS
      FETCH NEXT 10 ROWS ONLY ;`

Upvotes: 0

colm.anseo
colm.anseo

Reputation: 22147

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

Upvotes: 1

Dmitry Harnitski
Dmitry Harnitski

Reputation: 6018

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.

Upvotes: 0

Related Questions