-1

I am facing the above issue with the code below

    stmt, err2 := db.Prepare( "SELECT COUNT(*) FROM  xyz WHERE product_id=? and chart_number=?")
    rows, err2 := stmt.Query( bidStatusReqVal.ProductId,bidStatusReqVal.ChartNumber).Scan(&count)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
alsa
  • 9
  • 5
  • `Query().Scan()` ain't valid. Call `Scan` on the returned `rows`, or use `QueryRow().Scan()` with only `err` as the return destination. – mkopriva Apr 02 '21 at 05:38

1 Answers1

1

Query(...).Scan(...) is not valid because Query returns two values and chaining of calls requires that the previous call returns only one value. Call Scan on the returned rows, or use QueryRow(...).Scan(...) with only err as the return destination.

rows, err := stmt.Query(bidStatusReqVal.ProductId, bidStatusReqVal.ChartNumber)
if err != nil {
    return err
}
defer rows.Close()

for rows.Next() {
    if err := rows.Scan(&count); err != nil {
        return err
    }
}
if err := rows.Err(); err != nil {
    return err
}

// ...

In cases where the query returns only a single row, e.g. SELECT ... LIMIT 1, or SELECT COUNT(*) ... like in your case, it is much more convenient to use QueryRow.

err := stmt.QueryRow(bidStatusReqVal.ProductId, bidStatusReqVal.ChartNumber).Scan(&count)
if err != nil {
    return err
}

// ...
mkopriva
  • 35,176
  • 4
  • 57
  • 71