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)
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)
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
}
// ...